OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / f / bad.c
1 /* bad.c -- Implementation File (module.c template V1.0)
2    Copyright (C) 1995 Free Software Foundation, Inc.
3    Contributed by James Craig Burley.
4
5 This file is part of GNU Fortran.
6
7 GNU Fortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Fortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Fortran; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21
22    Related Modules:
23       None
24
25    Description:
26       Handles the displaying of diagnostic messages regarding the user's source
27       files.
28
29    Modifications:
30 */
31
32 /* If there's a %E or %4 in the messages, set this to at least 5,
33    for example.  */
34
35 #define FFEBAD_MAX_ 6
36
37 /* Include files. */
38
39 #include "proj.h"
40 #include "bad.h"
41 #include "flags.h"
42 #include "com.h"
43 #include "toplev.h"
44 #include "where.h"
45
46 /* Externals defined here. */
47
48 bool ffebad_is_inhibited_ = FALSE;
49
50 /* Simple definitions and enumerations. */
51
52 #define FFEBAD_LONG_MSGS_ 1     /* 0 to use short (or same) messages. */
53
54 /* Internal typedefs. */
55
56
57 /* Private include files. */
58
59
60 /* Internal structure definitions. */
61
62 struct _ffebad_message_
63   {
64     const ffebadSeverity severity;
65     const char *const message;
66   };
67
68 /* Static objects accessed by functions in this module.  */
69
70 static const struct _ffebad_message_ ffebad_messages_[]
71 =
72 {
73 #define FFEBAD_MSGS1(KWD,SEV,MSG) { SEV, MSG },
74 #if FFEBAD_LONG_MSGS_ == 0
75 #define FFEBAD_MSGS2(KWD,SEV,LMSG,SMSG) { SEV, SMSG },
76 #else
77 #define FFEBAD_MSGS2(KWD,SEV,LMSG,SMSG) { SEV, LMSG },
78 #endif
79 #include "bad.def"
80 #undef FFEBAD_MSGS1
81 #undef FFEBAD_MSGS2
82 };
83
84 static struct
85   {
86     ffewhereLine line;
87     ffewhereColumn col;
88     ffebadIndex tag;
89   }
90
91 ffebad_here_[FFEBAD_MAX_];
92 static const char *ffebad_string_[FFEBAD_MAX_];
93 static ffebadIndex ffebad_order_[FFEBAD_MAX_];
94 static ffebad ffebad_errnum_;
95 static ffebadSeverity ffebad_severity_;
96 static const char *ffebad_message_;
97 static unsigned char ffebad_index_;
98 static ffebadIndex ffebad_places_;
99 static bool ffebad_is_temp_inhibited_;  /* Effective setting of
100                                            _is_inhibited_ for this
101                                            _start/_finish invocation. */
102
103 /* Static functions (internal). */
104
105 static int ffebad_bufputs_ (char buf[], int bufi, const char *s);
106
107 /* Internal macros. */
108
109 #define ffebad_bufflush_(buf, bufi) \
110   (((buf)[bufi] = '\0'), fputs ((buf), stderr), 0)
111 #define ffebad_bufputc_(buf, bufi, c) \
112   (((bufi) == ARRAY_SIZE (buf)) \
113    ? (ffebad_bufflush_ ((buf), (bufi)), ((buf)[0] = (c)), 1) \
114    : (((buf)[bufi] = (c)), (bufi) + 1))
115 \f
116
117 static int
118 ffebad_bufputs_ (char buf[], int bufi, const char *s)
119 {
120   for (; *s != '\0'; ++s)
121     bufi = ffebad_bufputc_ (buf, bufi, *s);
122   return bufi;
123 }
124
125 /* ffebad_init_0 -- Initialize
126
127    ffebad_init_0();  */
128
129 void
130 ffebad_init_0 ()
131 {
132   assert (FFEBAD == ARRAY_SIZE (ffebad_messages_));
133 }
134
135 ffebadSeverity
136 ffebad_severity (ffebad errnum)
137 {
138   return ffebad_messages_[errnum].severity;
139 }
140
141 /* ffebad_start_ -- Start displaying an error message
142
143    ffebad_start(FFEBAD_SOME_ERROR_CODE);
144
145    Call ffebad_start to establish the message, ffebad_here and ffebad_string
146    to send run-time data to it as necessary, then ffebad_finish when through
147    to actually get it to print (to stderr).
148
149    Note: ffebad_start(errnum) turns into ffebad_start_(FALSE,errnum).  No
150    outside caller should call ffebad_start_ directly (as indicated by the
151    trailing underscore).
152
153    Call ffebad_start to start a normal message, one that might be inhibited
154    by the current state of statement guessing.  Call ffebad_start_lex
155    instead to start a message that is global to all statement guesses and
156    happens only once for all guesses (i.e. the lexer).
157
158    sev and message are overrides for the severity and messages when errnum
159    is FFEBAD, meaning the caller didn't want to have to put a message in
160    bad.def to produce a diagnostic.  */
161
162 bool
163 ffebad_start_ (bool lex_override, ffebad errnum, ffebadSeverity sev,
164                const char *message)
165 {
166   unsigned char i;
167
168   if (ffebad_is_inhibited_ && !lex_override)
169     {
170       ffebad_is_temp_inhibited_ = TRUE;
171       return FALSE;
172     }
173
174   if (errnum != FFEBAD)
175     {
176       ffebad_severity_ = ffebad_messages_[errnum].severity;
177       ffebad_message_ = ffebad_messages_[errnum].message;
178     }
179   else
180     {
181       ffebad_severity_ = sev;
182       ffebad_message_ = message;
183     }
184
185   switch (ffebad_severity_)
186     {                           /* Tell toplev.c about this message. */
187     case FFEBAD_severityINFORMATIONAL:
188     case FFEBAD_severityTRIVIAL:
189       if (inhibit_warnings)
190         {                       /* User wants no warnings. */
191           ffebad_is_temp_inhibited_ = TRUE;
192           return FALSE;
193         }
194       /* Fall through.  */
195     case FFEBAD_severityWARNING:
196     case FFEBAD_severityPECULIAR:
197     case FFEBAD_severityPEDANTIC:
198       if ((ffebad_severity_ != FFEBAD_severityPEDANTIC)
199           || !flag_pedantic_errors)
200         {
201           if (count_error (1) == 0)
202             {                   /* User wants no warnings. */
203               ffebad_is_temp_inhibited_ = TRUE;
204               return FALSE;
205             }
206           break;
207         }
208       /* Fall through (PEDANTIC && flag_pedantic_errors).  */
209     case FFEBAD_severityFATAL:
210     case FFEBAD_severityWEIRD:
211     case FFEBAD_severitySEVERE:
212     case FFEBAD_severityDISASTER:
213       count_error (0);
214       break;
215
216     default:
217       break;
218     }
219
220   ffebad_is_temp_inhibited_ = FALSE;
221   ffebad_errnum_ = errnum;
222   ffebad_index_ = 0;
223   ffebad_places_ = 0;
224   for (i = 0; i < FFEBAD_MAX_; ++i)
225     {
226       ffebad_string_[i] = NULL;
227       ffebad_here_[i].line = ffewhere_line_unknown ();
228       ffebad_here_[i].col = ffewhere_column_unknown ();
229     }
230
231   return TRUE;
232 }
233
234 /* ffebad_here -- Establish source location of some diagnostic concern
235
236    ffebad_here(ffebadIndex i,ffewhereLine line,ffewhereColumn col);
237
238    Call ffebad_start to establish the message, ffebad_here and ffebad_string
239    to send run-time data to it as necessary, then ffebad_finish when through
240    to actually get it to print (to stderr).  */
241
242 void
243 ffebad_here (ffebadIndex index, ffewhereLine line, ffewhereColumn col)
244 {
245   ffewhereLineNumber line_num;
246   ffewhereLineNumber ln;
247   ffewhereColumnNumber col_num;
248   ffewhereColumnNumber cn;
249   ffebadIndex i;
250   ffebadIndex j;
251
252   if (ffebad_is_temp_inhibited_)
253     return;
254
255   assert (index < FFEBAD_MAX_);
256   ffebad_here_[index].line = ffewhere_line_use (line);
257   ffebad_here_[index].col = ffewhere_column_use (col);
258   if (ffewhere_line_is_unknown (line)
259       || ffewhere_column_is_unknown (col))
260     {
261       ffebad_here_[index].tag = FFEBAD_MAX_;
262       return;
263     }
264   ffebad_here_[index].tag = 0;  /* For now, though it shouldn't matter. */
265
266   /* Sort the source line/col points into the order they occur in the source
267      file.  Deal with duplicates appropriately. */
268
269   line_num = ffewhere_line_number (line);
270   col_num = ffewhere_column_number (col);
271
272   /* Determine where in the ffebad_order_ array this new place should go. */
273
274   for (i = 0; i < ffebad_places_; ++i)
275     {
276       ln = ffewhere_line_number (ffebad_here_[ffebad_order_[i]].line);
277       cn = ffewhere_column_number (ffebad_here_[ffebad_order_[i]].col);
278       if (line_num < ln)
279         break;
280       if (line_num == ln)
281         {
282           if (col_num == cn)
283             {
284               ffebad_here_[index].tag = i;
285               return;           /* Shouldn't go in, has equivalent. */
286             }
287           else if (col_num < cn)
288             break;
289         }
290     }
291
292   /* Before putting new place in ffebad_order_[i], first increment all tags
293      that are i or greater. */
294
295   if (i != ffebad_places_)
296     {
297       for (j = 0; j < FFEBAD_MAX_; ++j)
298         {
299           if (ffebad_here_[j].tag >= i)
300             ++ffebad_here_[j].tag;
301         }
302     }
303
304   /* Then slide all ffebad_order_[] entries at and above i up one entry. */
305
306   for (j = ffebad_places_; j > i; --j)
307     ffebad_order_[j] = ffebad_order_[j - 1];
308
309   /* Finally can put new info in ffebad_order_[i]. */
310
311   ffebad_order_[i] = index;
312   ffebad_here_[index].tag = i;
313   ++ffebad_places_;
314 }
315
316 /* Establish string for next index (always in order) of message
317
318    ffebad_string(const char *string);
319
320    Call ffebad_start to establish the message, ffebad_here and ffebad_string
321    to send run-time data to it as necessary, then ffebad_finish when through
322    to actually get it to print (to stderr).  Note: don't trash the string
323    until after calling ffebad_finish, since we just maintain a pointer to
324    the argument passed in until then.  */
325
326 void
327 ffebad_string (const char *string)
328 {
329   if (ffebad_is_temp_inhibited_)
330     return;
331
332   assert (ffebad_index_ != FFEBAD_MAX_);
333   ffebad_string_[ffebad_index_++] = string;
334 }
335
336 /* ffebad_finish -- Display error message with where & run-time info
337
338    ffebad_finish();
339
340    Call ffebad_start to establish the message, ffebad_here and ffebad_string
341    to send run-time data to it as necessary, then ffebad_finish when through
342    to actually get it to print (to stderr).  */
343
344 void
345 ffebad_finish ()
346 {
347 #define MAX_SPACES 132
348   static const char *const spaces
349   = "...>\
350 \040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\
351 \040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\
352 \040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\
353 \040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\
354 \040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\
355 \040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\
356 \040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\
357 \040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\
358 \040\040\040";                  /* MAX_SPACES - 1 spaces. */
359   ffewhereLineNumber last_line_num;
360   ffewhereLineNumber ln;
361   ffewhereLineNumber rn;
362   ffewhereColumnNumber last_col_num;
363   ffewhereColumnNumber cn;
364   ffewhereColumnNumber cnt;
365   ffewhereLine l;
366   ffebadIndex bi;
367   unsigned short i;
368   char pointer;
369   unsigned char c;
370   unsigned const char *s;
371   const char *fn;
372   static char buf[1024];
373   int bufi;
374   int index;
375
376   if (ffebad_is_temp_inhibited_)
377     return;
378
379   switch (ffebad_severity_)
380     {
381     case FFEBAD_severityINFORMATIONAL:
382       s = "note:";
383       break;
384
385     case FFEBAD_severityWARNING:
386       s = "warning:";
387       break;
388
389     case FFEBAD_severitySEVERE:
390       s = "fatal:";
391       break;
392
393     default:
394       s = "";
395       break;
396     }
397
398   /* Display the annoying source references. */
399
400   last_line_num = 0;
401   last_col_num = 0;
402
403   for (bi = 0; bi < ffebad_places_; ++bi)
404     {
405       if (ffebad_places_ == 1)
406         pointer = '^';
407       else
408         pointer = '1' + bi;
409
410       l = ffebad_here_[ffebad_order_[bi]].line;
411       ln = ffewhere_line_number (l);
412       rn = ffewhere_line_filelinenum (l);
413       cn = ffewhere_column_number (ffebad_here_[ffebad_order_[bi]].col);
414       fn = ffewhere_line_filename (l);
415       if (ln != last_line_num)
416         {
417           if (bi != 0)
418             fputc ('\n', stderr);
419           report_error_function (fn);
420           fprintf (stderr,
421                    /* the trailing space on the <file>:<line>: line
422                       fools emacs19 compilation mode into finding the
423                       report */
424                    "%s:%" ffewhereLineNumber_f "u: %s\n   %s\n   %s%c",
425                    fn, rn,
426                    s,
427                    ffewhere_line_content (l),
428                    &spaces[cn > MAX_SPACES ? 0 : MAX_SPACES - cn + 4],
429                    pointer);
430           last_line_num = ln;
431           last_col_num = cn;
432           s = "(continued):";
433         }
434       else
435         {
436           cnt = cn - last_col_num;
437           fprintf (stderr,
438                    "%s%c", &spaces[cnt > MAX_SPACES
439                                    ? 0 : MAX_SPACES - cnt + 4],
440                    pointer);
441           last_col_num = cn;
442         }
443     }
444   if (ffebad_places_ == 0)
445     {
446       /* Didn't output "warning:" string, capitalize it for message.  */
447       if (s[0] != '\0')
448         {
449           char c;
450
451           c = TOUPPER (s[0]);
452           fprintf (stderr, "%c%s ", c, &s[1]);
453         }
454       else if (s[0] != '\0')
455         fprintf (stderr, "%s ", s);
456     }
457   else
458     fputc ('\n', stderr);
459
460   /* Release the ffewhere info. */
461
462   for (bi = 0; bi < FFEBAD_MAX_; ++bi)
463     {
464       ffewhere_line_kill (ffebad_here_[bi].line);
465       ffewhere_column_kill (ffebad_here_[bi].col);
466     }
467
468   /* Now display the message. */
469
470   bufi = 0;
471   for (i = 0; (c = ffebad_message_[i]) != '\0'; ++i)
472     {
473       if (c == '%')
474         {
475           c = ffebad_message_[++i];
476           if (ISUPPER (c))
477             {
478               index = c - 'A';
479
480               if ((index < 0) || (index >= FFEBAD_MAX_))
481                 {
482                   bufi = ffebad_bufputs_ (buf, bufi, "[REPORT BUG!!] %");
483                   bufi = ffebad_bufputc_ (buf, bufi, c);
484                 }
485               else
486                 {
487                   s = ffebad_string_[index];
488                   if (s == NULL)
489                     bufi = ffebad_bufputs_ (buf, bufi, "[REPORT BUG!!]");
490                   else
491                     bufi = ffebad_bufputs_ (buf, bufi, s);
492                 }
493             }
494           else if (ISDIGIT (c))
495             {
496               index = c - '0';
497
498               if ((index < 0) || (index >= FFEBAD_MAX_))
499                 {
500                   bufi = ffebad_bufputs_ (buf, bufi, "[REPORT BUG!!] %");
501                   bufi = ffebad_bufputc_ (buf, bufi, c);
502                 }
503               else
504                 {
505                   pointer = ffebad_here_[index].tag + '1';
506                   if (pointer == FFEBAD_MAX_ + '1')
507                     pointer = '?';
508                   else if (ffebad_places_ == 1)
509                     pointer = '^';
510                   bufi = ffebad_bufputc_ (buf, bufi, '(');
511                   bufi = ffebad_bufputc_ (buf, bufi, pointer);
512                   bufi = ffebad_bufputc_ (buf, bufi, ')');
513                 }
514             }
515           else if (c == '\0')
516             break;
517           else if (c == '%')
518             bufi = ffebad_bufputc_ (buf, bufi, '%');
519           else
520             {
521               bufi = ffebad_bufputs_ (buf, bufi, "[REPORT BUG!!]");
522               bufi = ffebad_bufputc_ (buf, bufi, '%');
523               bufi = ffebad_bufputc_ (buf, bufi, c);
524             }
525         }
526       else
527         bufi = ffebad_bufputc_ (buf, bufi, c);
528     }
529   bufi = ffebad_bufputc_ (buf, bufi, '\n');
530   bufi = ffebad_bufflush_ (buf, bufi);
531 }