OSDN Git Service

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