PR middle-end/61455
[official-gcc.git] / gcc / ada / errutil.adb
blob7eb85a4193aa40c05673eb83ef61c05b09a54a9e
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E R R U T I L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1991-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Err_Vars; use Err_Vars;
28 with Erroutc; use Erroutc;
29 with Namet; use Namet;
30 with Opt; use Opt;
31 with Output; use Output;
32 with Scans; use Scans;
33 with Sinput; use Sinput;
34 with Stringt; use Stringt;
35 with Stylesw; use Stylesw;
37 package body Errutil is
39 Errors_Must_Be_Ignored : Boolean := False;
40 -- Set to True by procedure Set_Ignore_Errors (True), when calls to
41 -- error message procedures should be ignored (when parsing irrelevant
42 -- text in sources being preprocessed).
44 -----------------------
45 -- Local Subprograms --
46 -----------------------
48 procedure Error_Msg_AP (Msg : String);
49 -- Output a message just after the previous token
51 procedure Output_Source_Line
52 (L : Physical_Line_Number;
53 Sfile : Source_File_Index;
54 Errs : Boolean;
55 Source_Type : String);
56 -- Outputs text of source line L, in file S, together with preceding line
57 -- number, as described above for Output_Line_Number. The Errs parameter
58 -- indicates if there are errors attached to the line, which forces
59 -- listing on, even in the presence of pragma List (Off).
61 procedure Set_Msg_Insertion_Column;
62 -- Handle column number insertion (@ insertion character)
64 procedure Set_Msg_Text (Text : String; Flag : Source_Ptr);
65 -- Add a sequence of characters to the current message. The characters may
66 -- be one of the special insertion characters (see documentation in spec).
67 -- Flag is the location at which the error is to be posted, which is used
68 -- to determine whether or not the # insertion needs a file name. The
69 -- variables Msg_Buffer, Msglen, Is_Style_Msg, Is_Warning_Msg, and
70 -- Is_Unconditional_Msg are set on return.
72 ------------------
73 -- Error_Msg_AP --
74 ------------------
76 procedure Error_Msg_AP (Msg : String) is
77 S1 : Source_Ptr;
78 C : Character;
80 begin
81 -- If we had saved the Scan_Ptr value after scanning the previous
82 -- token, then we would have exactly the right place for putting
83 -- the flag immediately at hand. However, that would add at least
84 -- two instructions to a Scan call *just* to service the possibility
85 -- of an Error_Msg_AP call. So instead we reconstruct that value.
87 -- We have two possibilities, start with Prev_Token_Ptr and skip over
88 -- the current token, which is made harder by the possibility that this
89 -- token may be in error, or start with Token_Ptr and work backwards.
90 -- We used to take the second approach, but it's hard because of
91 -- comments, and harder still because things that look like comments
92 -- can appear inside strings. So now we take the first approach.
94 -- Note: in the case where there is no previous token, Prev_Token_Ptr
95 -- is set to Source_First, which is a reasonable position for the
96 -- error flag in this situation.
98 S1 := Prev_Token_Ptr;
99 C := Source (S1);
101 -- If the previous token is a string literal, we need a special approach
102 -- since there may be white space inside the literal and we don't want
103 -- to stop on that white space.
105 -- Note that it is not worth worrying about special UTF_32 line
106 -- terminator characters in this context, since this is only about
107 -- error recovery anyway.
109 if Prev_Token = Tok_String_Literal then
110 loop
111 S1 := S1 + 1;
113 if Source (S1) = C then
114 S1 := S1 + 1;
115 exit when Source (S1) /= C;
116 elsif Source (S1) in Line_Terminator then
117 exit;
118 end if;
119 end loop;
121 -- Character literal also needs special handling
123 elsif Prev_Token = Tok_Char_Literal then
124 S1 := S1 + 3;
126 -- Otherwise we search forward for the end of the current token, marked
127 -- by a line terminator, white space, a comment symbol or if we bump
128 -- into the following token (i.e. the current token)
130 -- Note that it is not worth worrying about special UTF_32 line
131 -- terminator characters in this context, since this is only about
132 -- error recovery anyway.
134 else
135 while Source (S1) not in Line_Terminator
136 and then Source (S1) /= ' '
137 and then Source (S1) /= ASCII.HT
138 and then (Source (S1) /= '-' or else Source (S1 + 1) /= '-')
139 and then S1 /= Token_Ptr
140 loop
141 S1 := S1 + 1;
142 end loop;
143 end if;
145 -- S1 is now set to the location for the flag
147 Error_Msg (Msg, S1);
149 end Error_Msg_AP;
151 ---------------
152 -- Error_Msg --
153 ---------------
155 procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr) is
157 Next_Msg : Error_Msg_Id;
158 -- Pointer to next message at insertion point
160 Prev_Msg : Error_Msg_Id;
161 -- Pointer to previous message at insertion point
163 Sptr : Source_Ptr renames Flag_Location;
164 -- Corresponds to the Sptr value in the error message object
166 Optr : Source_Ptr renames Flag_Location;
167 -- Corresponds to the Optr value in the error message object. Note that
168 -- for this usage, Sptr and Optr always have the same value, since we do
169 -- not have to worry about generic instantiations.
171 begin
172 if Errors_Must_Be_Ignored then
173 return;
174 end if;
176 if Raise_Exception_On_Error /= 0 then
177 raise Error_Msg_Exception;
178 end if;
180 Prescan_Message (Msg);
181 Set_Msg_Text (Msg, Sptr);
183 -- Kill continuation if parent message killed
185 if Continuation and Last_Killed then
186 return;
187 end if;
189 -- Return without doing anything if message is killed and this is not
190 -- the first error message. The philosophy is that if we get a weird
191 -- error message and we already have had a message, then we hope the
192 -- weird message is a junk cascaded message
194 -- Immediate return if warning message and warnings are suppressed.
195 -- Note that style messages are not warnings for this purpose.
197 if Is_Warning_Msg and then Warnings_Suppressed (Sptr) /= No_String then
198 Cur_Msg := No_Error_Msg;
199 return;
200 end if;
202 -- Otherwise build error message object for new message
204 Errors.Append
205 (New_Val =>
206 (Text => new String'(Msg_Buffer (1 .. Msglen)),
207 Next => No_Error_Msg,
208 Prev => No_Error_Msg,
209 Sfile => Get_Source_File_Index (Sptr),
210 Sptr => Sptr,
211 Optr => Optr,
212 Line => Get_Physical_Line_Number (Sptr),
213 Col => Get_Column_Number (Sptr),
214 Warn => Is_Warning_Msg,
215 Info => Is_Info_Msg,
216 Warn_Err => Warning_Mode = Treat_As_Error,
217 Warn_Chr => Warning_Msg_Char,
218 Style => Is_Style_Msg,
219 Serious => Is_Serious_Error,
220 Uncond => Is_Unconditional_Msg,
221 Msg_Cont => Continuation,
222 Deleted => False));
224 Cur_Msg := Errors.Last;
225 Prev_Msg := No_Error_Msg;
226 Next_Msg := First_Error_Msg;
228 while Next_Msg /= No_Error_Msg loop
229 exit when
230 Errors.Table (Cur_Msg).Sfile < Errors.Table (Next_Msg).Sfile;
232 if Errors.Table (Cur_Msg).Sfile = Errors.Table (Next_Msg).Sfile then
233 exit when Sptr < Errors.Table (Next_Msg).Sptr;
234 end if;
236 Prev_Msg := Next_Msg;
237 Next_Msg := Errors.Table (Next_Msg).Next;
238 end loop;
240 -- Now we insert the new message in the error chain. The insertion
241 -- point for the message is after Prev_Msg and before Next_Msg.
243 -- The possible insertion point for the new message is after Prev_Msg
244 -- and before Next_Msg. However, this is where we do a special check
245 -- for redundant parsing messages, defined as messages posted on the
246 -- same line. The idea here is that probably such messages are junk
247 -- from the parser recovering. In full errors mode, we don't do this
248 -- deletion, but otherwise such messages are discarded at this stage.
250 if Prev_Msg /= No_Error_Msg
251 and then Errors.Table (Prev_Msg).Line =
252 Errors.Table (Cur_Msg).Line
253 and then Errors.Table (Prev_Msg).Sfile =
254 Errors.Table (Cur_Msg).Sfile
255 then
256 -- Don't delete unconditional messages and at this stage, don't
257 -- delete continuation lines (we attempted to delete those earlier
258 -- if the parent message was deleted.
260 if not Errors.Table (Cur_Msg).Uncond
261 and then not Continuation
262 then
264 -- Don't delete if prev msg is warning and new msg is an error.
265 -- This is because we don't want a real error masked by a warning.
266 -- In all other cases (that is parse errors for the same line that
267 -- are not unconditional) we do delete the message. This helps to
268 -- avoid junk extra messages from cascaded parsing errors
270 if not (Errors.Table (Prev_Msg).Warn
271 or else
272 Errors.Table (Prev_Msg).Style)
273 or else
274 (Errors.Table (Cur_Msg).Warn
275 or else
276 Errors.Table (Cur_Msg).Style)
277 then
278 -- All tests passed, delete the message by simply returning
279 -- without any further processing.
281 if not Continuation then
282 Last_Killed := True;
283 end if;
285 return;
286 end if;
287 end if;
288 end if;
290 -- Come here if message is to be inserted in the error chain
292 if not Continuation then
293 Last_Killed := False;
294 end if;
296 if Prev_Msg = No_Error_Msg then
297 First_Error_Msg := Cur_Msg;
298 else
299 Errors.Table (Prev_Msg).Next := Cur_Msg;
300 end if;
302 Errors.Table (Cur_Msg).Next := Next_Msg;
304 -- Bump appropriate statistics count
306 if Errors.Table (Cur_Msg).Warn
307 or else
308 Errors.Table (Cur_Msg).Style
309 then
310 Warnings_Detected := Warnings_Detected + 1;
312 if Errors.Table (Cur_Msg).Info then
313 Info_Messages := Info_Messages + 1;
314 end if;
316 else
317 Total_Errors_Detected := Total_Errors_Detected + 1;
319 if Errors.Table (Cur_Msg).Serious then
320 Serious_Errors_Detected := Serious_Errors_Detected + 1;
321 end if;
322 end if;
324 end Error_Msg;
326 -----------------
327 -- Error_Msg_S --
328 -----------------
330 procedure Error_Msg_S (Msg : String) is
331 begin
332 Error_Msg (Msg, Scan_Ptr);
333 end Error_Msg_S;
335 ------------------
336 -- Error_Msg_SC --
337 ------------------
339 procedure Error_Msg_SC (Msg : String) is
340 begin
341 -- If we are at end of file, post the flag after the previous token
343 if Token = Tok_EOF then
344 Error_Msg_AP (Msg);
346 -- For all other cases the message is posted at the current token
347 -- pointer position
349 else
350 Error_Msg (Msg, Token_Ptr);
351 end if;
352 end Error_Msg_SC;
354 ------------------
355 -- Error_Msg_SP --
356 ------------------
358 procedure Error_Msg_SP (Msg : String) is
359 begin
360 -- Note: in the case where there is no previous token, Prev_Token_Ptr
361 -- is set to Source_First, which is a reasonable position for the
362 -- error flag in this situation
364 Error_Msg (Msg, Prev_Token_Ptr);
365 end Error_Msg_SP;
367 --------------
368 -- Finalize --
369 --------------
371 procedure Finalize (Source_Type : String := "project") is
372 Cur : Error_Msg_Id;
373 Nxt : Error_Msg_Id;
374 E, F : Error_Msg_Id;
375 Err_Flag : Boolean;
377 begin
378 -- Eliminate any duplicated error messages from the list. This is
379 -- done after the fact to avoid problems with Change_Error_Text.
381 Cur := First_Error_Msg;
382 while Cur /= No_Error_Msg loop
383 Nxt := Errors.Table (Cur).Next;
385 F := Nxt;
386 while F /= No_Error_Msg
387 and then Errors.Table (F).Sptr = Errors.Table (Cur).Sptr
388 loop
389 Check_Duplicate_Message (Cur, F);
390 F := Errors.Table (F).Next;
391 end loop;
393 Cur := Nxt;
394 end loop;
396 -- Brief Error mode
398 if Brief_Output or (not Full_List and not Verbose_Mode) then
399 E := First_Error_Msg;
400 Set_Standard_Error;
402 while E /= No_Error_Msg loop
403 if not Errors.Table (E).Deleted then
404 if Full_Path_Name_For_Brief_Errors then
405 Write_Name (Full_Ref_Name (Errors.Table (E).Sfile));
406 else
407 Write_Name (Reference_Name (Errors.Table (E).Sfile));
408 end if;
410 Write_Char (':');
411 Write_Int (Int (Physical_To_Logical
412 (Errors.Table (E).Line,
413 Errors.Table (E).Sfile)));
414 Write_Char (':');
416 if Errors.Table (E).Col < 10 then
417 Write_Char ('0');
418 end if;
420 Write_Int (Int (Errors.Table (E).Col));
421 Write_Str (": ");
422 Output_Msg_Text (E);
423 Write_Eol;
424 end if;
426 E := Errors.Table (E).Next;
427 end loop;
429 Set_Standard_Output;
430 end if;
432 -- Full source listing case
434 if Full_List then
435 List_Pragmas_Index := 1;
436 List_Pragmas_Mode := True;
437 E := First_Error_Msg;
438 Write_Eol;
440 -- First list initial main source file with its error messages
442 for N in 1 .. Last_Source_Line (Main_Source_File) loop
443 Err_Flag :=
444 E /= No_Error_Msg
445 and then Errors.Table (E).Line = N
446 and then Errors.Table (E).Sfile = Main_Source_File;
448 Output_Source_Line (N, Main_Source_File, Err_Flag, Source_Type);
450 if Err_Flag then
451 Output_Error_Msgs (E);
453 Write_Eol;
454 end if;
455 end loop;
457 -- Then output errors, if any, for subsidiary units
459 while E /= No_Error_Msg
460 and then Errors.Table (E).Sfile /= Main_Source_File
461 loop
462 Write_Eol;
463 Output_Source_Line
464 (Errors.Table (E).Line,
465 Errors.Table (E).Sfile,
466 True,
467 Source_Type);
468 Output_Error_Msgs (E);
469 end loop;
470 end if;
472 -- Verbose mode (error lines only with error flags)
474 if Verbose_Mode then
475 E := First_Error_Msg;
477 -- Loop through error lines
479 while E /= No_Error_Msg loop
480 Write_Eol;
481 Output_Source_Line
482 (Errors.Table (E).Line,
483 Errors.Table (E).Sfile,
484 True,
485 Source_Type);
486 Output_Error_Msgs (E);
487 end loop;
488 end if;
490 -- Output error summary if verbose or full list mode
492 if Verbose_Mode or else Full_List then
494 -- Extra blank line if error messages or source listing were output
496 if Total_Errors_Detected + Warnings_Detected > 0
497 or else Full_List
498 then
499 Write_Eol;
500 end if;
502 -- Message giving number of lines read and number of errors detected.
503 -- This normally goes to Standard_Output. The exception is when brief
504 -- mode is not set, verbose mode (or full list mode) is set, and
505 -- there are errors. In this case we send the message to standard
506 -- error to make sure that *something* appears on standard error in
507 -- an error situation.
509 -- Historical note: Formerly, only the "# errors" suffix was sent
510 -- to stderr, whereas "# lines:" appeared on stdout. This caused
511 -- some problems on now-obsolete ports, but there seems to be no
512 -- reason to revert this page since it would be incompatible.
514 if Total_Errors_Detected + Warnings_Detected /= 0
515 and then not Brief_Output
516 and then (Verbose_Mode or Full_List)
517 then
518 Set_Standard_Error;
519 end if;
521 -- Message giving total number of lines
523 Write_Str (" ");
524 Write_Int (Num_Source_Lines (Main_Source_File));
526 if Num_Source_Lines (Main_Source_File) = 1 then
527 Write_Str (" line: ");
528 else
529 Write_Str (" lines: ");
530 end if;
532 if Total_Errors_Detected = 0 then
533 Write_Str ("No errors");
535 elsif Total_Errors_Detected = 1 then
536 Write_Str ("1 error");
538 else
539 Write_Int (Total_Errors_Detected);
540 Write_Str (" errors");
541 end if;
543 if Warnings_Detected - Info_Messages /= 0 then
544 Write_Str (", ");
545 Write_Int (Warnings_Detected - Info_Messages);
546 Write_Str (" warning");
548 if Warnings_Detected - Info_Messages /= 1 then
549 Write_Char ('s');
550 end if;
552 if Warning_Mode = Treat_As_Error then
553 Write_Str (" (treated as error");
555 if Warnings_Detected - Info_Messages /= 1 then
556 Write_Char ('s');
557 end if;
559 Write_Char (')');
560 end if;
561 end if;
563 Write_Eol;
564 Set_Standard_Output;
565 end if;
567 if Maximum_Messages /= 0 then
568 if Warnings_Detected >= Maximum_Messages then
569 Set_Standard_Error;
570 Write_Line ("maximum number of warnings detected");
571 Warning_Mode := Suppress;
572 end if;
574 if Total_Errors_Detected >= Maximum_Messages then
575 Set_Standard_Error;
576 Write_Line ("fatal error: maximum errors reached");
577 Set_Standard_Output;
578 end if;
579 end if;
581 if Warning_Mode = Treat_As_Error then
582 Total_Errors_Detected :=
583 Total_Errors_Detected + Warnings_Detected - Info_Messages;
584 Warnings_Detected := Info_Messages;
585 end if;
587 -- Prevent displaying the same messages again in the future
589 First_Error_Msg := No_Error_Msg;
590 end Finalize;
592 ----------------
593 -- Initialize --
594 ----------------
596 procedure Initialize is
597 begin
598 Errors.Init;
599 First_Error_Msg := No_Error_Msg;
600 Last_Error_Msg := No_Error_Msg;
601 Serious_Errors_Detected := 0;
602 Total_Errors_Detected := 0;
603 Warnings_Detected := 0;
604 Info_Messages := 0;
605 Cur_Msg := No_Error_Msg;
607 -- Initialize warnings table, if all warnings are suppressed, supply
608 -- an initial dummy entry covering all possible source locations.
610 Warnings.Init;
612 if Warning_Mode = Suppress then
613 Warnings.Append
614 (New_Val =>
615 (Start => Source_Ptr'First,
616 Stop => Source_Ptr'Last,
617 Reason => Null_String_Id));
618 end if;
619 end Initialize;
621 ------------------------
622 -- Output_Source_Line --
623 ------------------------
625 procedure Output_Source_Line
626 (L : Physical_Line_Number;
627 Sfile : Source_File_Index;
628 Errs : Boolean;
629 Source_Type : String)
631 S : Source_Ptr;
632 C : Character;
634 Line_Number_Output : Boolean := False;
635 -- Set True once line number is output
637 begin
638 if Sfile /= Current_Error_Source_File then
639 Write_Str ("==============Error messages for ");
640 Write_Str (Source_Type);
641 Write_Str (" file: ");
642 Write_Name (Full_File_Name (Sfile));
643 Write_Eol;
644 Current_Error_Source_File := Sfile;
645 end if;
647 if Errs then
648 Output_Line_Number (Physical_To_Logical (L, Sfile));
649 Line_Number_Output := True;
650 end if;
652 S := Line_Start (L, Sfile);
654 loop
655 C := Source_Text (Sfile) (S);
656 exit when C = ASCII.LF or else C = ASCII.CR or else C = EOF;
658 if Errs then
659 Write_Char (C);
660 end if;
662 S := S + 1;
663 end loop;
665 if Line_Number_Output then
666 Write_Eol;
667 end if;
668 end Output_Source_Line;
670 -----------------------
671 -- Set_Ignore_Errors --
672 -----------------------
674 procedure Set_Ignore_Errors (To : Boolean) is
675 begin
676 Errors_Must_Be_Ignored := To;
677 end Set_Ignore_Errors;
679 ------------------------------
680 -- Set_Msg_Insertion_Column --
681 ------------------------------
683 procedure Set_Msg_Insertion_Column is
684 begin
685 if RM_Column_Check then
686 Set_Msg_Str (" in column ");
687 Set_Msg_Int (Int (Error_Msg_Col) + 1);
688 end if;
689 end Set_Msg_Insertion_Column;
691 ------------------
692 -- Set_Msg_Text --
693 ------------------
695 procedure Set_Msg_Text (Text : String; Flag : Source_Ptr) is
696 C : Character; -- Current character
697 P : Natural; -- Current index;
699 begin
700 Manual_Quote_Mode := False;
701 Msglen := 0;
702 Flag_Source := Get_Source_File_Index (Flag);
703 P := Text'First;
705 while P <= Text'Last loop
706 C := Text (P);
707 P := P + 1;
709 -- Check for insertion character
711 if C = '%' then
712 if P <= Text'Last and then Text (P) = '%' then
713 P := P + 1;
714 Set_Msg_Insertion_Name_Literal;
715 else
716 Set_Msg_Insertion_Name;
717 end if;
719 elsif C = '$' then
721 -- '$' is ignored
723 null;
725 elsif C = '{' then
726 Set_Msg_Insertion_File_Name;
728 elsif C = '}' then
730 -- '}' is ignored
732 null;
734 elsif C = '*' then
735 Set_Msg_Insertion_Reserved_Name;
737 elsif C = '&' then
739 -- '&' is ignored
741 null;
743 elsif C = '#' then
744 Set_Msg_Insertion_Line_Number (Error_Msg_Sloc, Flag);
746 elsif C = '\' then
747 Continuation := True;
749 elsif C = '@' then
750 Set_Msg_Insertion_Column;
752 elsif C = '^' then
753 Set_Msg_Insertion_Uint;
755 elsif C = '`' then
756 Manual_Quote_Mode := not Manual_Quote_Mode;
757 Set_Msg_Char ('"');
759 elsif C = '!' then
760 null;
762 elsif C = '?' then
763 null;
765 elsif C = '<' then
766 null;
768 elsif C = '|' then
769 null;
771 elsif C = ''' then
772 Set_Msg_Char (Text (P));
773 P := P + 1;
775 -- Upper case letter (start of reserved word if 2 or more)
777 elsif C in 'A' .. 'Z'
778 and then P <= Text'Last
779 and then Text (P) in 'A' .. 'Z'
780 then
781 P := P - 1;
782 Set_Msg_Insertion_Reserved_Word (Text, P);
784 elsif C = '~' then
785 Set_Msg_Str (Error_Msg_String (1 .. Error_Msg_Strlen));
787 -- Normal character with no special treatment
789 else
790 Set_Msg_Char (C);
791 end if;
793 end loop;
794 end Set_Msg_Text;
796 end Errutil;