OSDN Git Service

* config/m68k/m68k.c (output_move_const_into_data_reg,
[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  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 *, unsigned int, 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, unsigned int line, unsigned int col)
40 {
41   if (!pfile->buffer || line == 0)
42     fprintf (stderr, "%s: ", progname);
43   else
44     {
45       const struct line_map *map;
46
47       map = linemap_lookup (&pfile->line_maps, line);
48       linemap_print_containing_files (&pfile->line_maps, map);
49
50       line = SOURCE_LINE (map, line);
51       if (col == 0)
52         col = 1;
53
54       if (line == 0)
55         fprintf (stderr, "%s:", map->to_file);
56       else if (CPP_OPTION (pfile, show_column) == 0)
57         fprintf (stderr, "%s:%u:", map->to_file, line);
58       else
59         fprintf (stderr, "%s:%u:%u:", map->to_file, line, col);
60
61       fputc (' ', stderr);
62     }
63 }
64
65 /* Set up for a diagnostic: print the file and line, bump the error
66    counter, etc.  LINE is the logical line number; zero means to print
67    at the location of the previously lexed token, which tends to be
68    the correct place by default.  Returns 0 if the error has been
69    suppressed.  */
70 int
71 _cpp_begin_message (cpp_reader *pfile, int code, unsigned int line,
72                     unsigned int column)
73 {
74   int level = DL_EXTRACT (code);
75
76   switch (level)
77     {
78     case DL_WARNING:
79     case DL_PEDWARN:
80       if (CPP_IN_SYSTEM_HEADER (pfile)
81           && ! CPP_OPTION (pfile, warn_system_headers))
82         return 0;
83       /* Fall through.  */
84
85     case DL_WARNING_SYSHDR:
86       if (CPP_OPTION (pfile, warnings_are_errors)
87           || (level == DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
88         {
89           if (CPP_OPTION (pfile, inhibit_errors))
90             return 0;
91           level = DL_ERROR;
92           pfile->errors++;
93         }
94       else if (CPP_OPTION (pfile, inhibit_warnings))
95         return 0;
96       break;
97
98     case DL_ERROR:
99       if (CPP_OPTION (pfile, inhibit_errors))
100         return 0;
101       /* ICEs cannot be inhibited.  */
102     case DL_ICE:
103       pfile->errors++;
104       break;
105     }
106
107   print_location (pfile, line, column);
108   if (DL_WARNING_P (level))
109     fputs (_("warning: "), stderr);
110   else if (level == DL_ICE)
111     fputs (_("internal error: "), stderr);
112
113   return 1;
114 }
115
116 /* Don't remove the blank before do, as otherwise the exgettext
117    script will mistake this as a function definition */
118 #define v_message(msgid, ap) \
119  do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
120
121 /* Exported interface.  */
122
123 /* Print an error at the location of the previously lexed token.  */
124 void
125 cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
126 {
127   unsigned int line, column;
128   va_list ap;
129   
130   va_start (ap, msgid);
131
132   if (pfile->buffer)
133     {
134       if (CPP_OPTION (pfile, traditional))
135         {
136           if (pfile->state.in_directive)
137             line = pfile->directive_line;
138           else
139             line = pfile->line;
140           column = 0;
141         }
142       else
143         {
144           line = pfile->cur_token[-1].line;
145           column = pfile->cur_token[-1].col;
146         }
147     }
148   else
149     line = column = 0;
150
151   if (_cpp_begin_message (pfile, level, line, column))
152     v_message (msgid, ap);
153
154   va_end (ap);
155 }
156
157 /* Print an error at a specific location.  */
158 void
159 cpp_error_with_line (cpp_reader *pfile, int level,
160                      unsigned int line, unsigned int column,
161                      const char *msgid, ...)
162 {
163   va_list ap;
164   
165   va_start (ap, msgid);
166
167   if (_cpp_begin_message (pfile, level, line, column))
168     v_message (msgid, ap);
169
170   va_end (ap);
171 }
172
173 void
174 cpp_errno (cpp_reader *pfile, int level, const char *msgid)
175 {
176   if (msgid[0] == '\0')
177     msgid = _("stdout");
178
179   cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));
180 }