OSDN Git Service

2004-05-13 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / cpperror.c
1 /* Default error handlers for CPP Library.
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000,
3    2001, 2002, 2004 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
7
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.
12
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.
17
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.
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
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30 #include "intl.h"
31
32 static void print_location (cpp_reader *, source_location, unsigned int);
33
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, source_location line, unsigned int col)
40 {
41   if (line == 0)
42     fprintf (stderr, "%s: ", progname);
43   else
44     {
45       const struct line_map *map;
46       unsigned int lin;
47
48       map = linemap_lookup (pfile->line_table, line);
49       linemap_print_containing_files (pfile->line_table, map);
50
51       lin = SOURCE_LINE (map, line);
52       if (col == 0)
53         {
54           col = SOURCE_COLUMN (map, line);
55           if (col == 0)
56             col = 1;
57         }
58
59       if (lin == 0)
60         fprintf (stderr, "%s:", map->to_file);
61       else if (CPP_OPTION (pfile, show_column) == 0)
62         fprintf (stderr, "%s:%u:", map->to_file, lin);
63       else
64         fprintf (stderr, "%s:%u:%u:", map->to_file, lin, col);
65
66       fputc (' ', stderr);
67     }
68 }
69
70 /* Set up for a diagnostic: print the file and line, bump the error
71    counter, etc.  SRC_LOC is the logical line number; zero means to print
72    at the location of the previously lexed token, which tends to be
73    the correct place by default.  The column number can be specified either
74    using COLUMN or (if COLUMN==0) extracting SOURCE_COLUMN from SRC_LOC.
75    (This may seem redundant, but is useful when pre-scanning (cleaning) a line,
76    when we haven't yet verified whether the current line_map has a
77    big enough max_column_hint.)
78
79    Returns 0 if the error has been suppressed.  */
80 int
81 _cpp_begin_message (cpp_reader *pfile, int code,
82                     source_location src_loc, unsigned int column)
83 {
84   int level = CPP_DL_EXTRACT (code);
85
86   switch (level)
87     {
88     case CPP_DL_WARNING:
89     case CPP_DL_PEDWARN:
90       if (cpp_in_system_header (pfile)
91           && ! CPP_OPTION (pfile, warn_system_headers))
92         return 0;
93       /* Fall through.  */
94
95     case CPP_DL_WARNING_SYSHDR:
96       if (CPP_OPTION (pfile, warnings_are_errors)
97           || (level == CPP_DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
98         {
99           if (CPP_OPTION (pfile, inhibit_errors))
100             return 0;
101           level = CPP_DL_ERROR;
102           pfile->errors++;
103         }
104       else if (CPP_OPTION (pfile, inhibit_warnings))
105         return 0;
106       break;
107
108     case CPP_DL_ERROR:
109       if (CPP_OPTION (pfile, inhibit_errors))
110         return 0;
111       /* ICEs cannot be inhibited.  */
112     case CPP_DL_ICE:
113       pfile->errors++;
114       break;
115     }
116
117   print_location (pfile, src_loc, column);
118   if (CPP_DL_WARNING_P (level))
119     fputs (_("warning: "), stderr);
120   else if (level == CPP_DL_ICE)
121     fputs (_("internal error: "), stderr);
122
123   return 1;
124 }
125
126 /* Don't remove the blank before do, as otherwise the exgettext
127    script will mistake this as a function definition */
128 #define v_message(msgid, ap) \
129  do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
130
131 /* Exported interface.  */
132
133 /* Print an error at the location of the previously lexed token.  */
134 void
135 cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
136 {
137   source_location src_loc;
138   va_list ap;
139   
140   va_start (ap, msgid);
141
142   if (CPP_OPTION (pfile, traditional))
143     {
144       if (pfile->state.in_directive)
145         src_loc = pfile->directive_line;
146       else
147         src_loc = pfile->line_table->highest_line;
148     }
149   else
150     {
151       src_loc = pfile->cur_token[-1].src_loc;
152     }
153
154   if (_cpp_begin_message (pfile, level, src_loc, 0))
155     v_message (msgid, ap);
156
157   va_end (ap);
158 }
159
160 /* Print an error at a specific location.  */
161 void
162 cpp_error_with_line (cpp_reader *pfile, int level,
163                      source_location src_loc, unsigned int column,
164                      const char *msgid, ...)
165 {
166   va_list ap;
167   
168   va_start (ap, msgid);
169
170   if (_cpp_begin_message (pfile, level, src_loc, column))
171     v_message (msgid, ap);
172
173   va_end (ap);
174 }
175
176 void
177 cpp_errno (cpp_reader *pfile, int level, const char *msgid)
178 {
179   if (msgid[0] == '\0')
180     msgid = _("stdout");
181
182   cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));
183 }