Debian Codegraph @

gcc/ada/errutil.adb

raw
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-2025, 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 ------------------------------------------------------------------------------
25
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
36 package body Errutil is
37
38 Errors_Must_Be_Ignored : Boolean := False;
39 -- Set to True by procedure Set_Ignore_Errors (True), when calls to
40 -- error message procedures should be ignored (when parsing irrelevant
41 -- text in sources being preprocessed).
42
43 -----------------------
44 -- Local Subprograms --
45 -----------------------
46
47 procedure Error_Msg_AP (Msg : String);
48 -- Output a message just after the previous token
49
50 procedure Output_Source_Line
51 (L : Physical_Line_Number;
52 Sfile : Source_File_Index;
53 Errs : Boolean;
54 Source_Type : String);
55 -- Outputs text of source line L, in file S, together with preceding line
56 -- number, as described above for Output_Line_Number. The Errs parameter
57 -- indicates if there are errors attached to the line, which forces
58 -- listing on, even in the presence of pragma List (Off).
59
60 procedure Set_Msg_Text (Text : String; Flag : Source_Ptr);
61 -- Add a sequence of characters to the current message. The characters may
62 -- be one of the special insertion characters (see documentation in spec).
63 -- Flag is the location at which the error is to be posted, which is used
64 -- to determine whether or not the # insertion needs a file name. The
65 -- variables Msg_Buffer, Msglen and Is_Unconditional_Msg are set on return.
66
67 ------------------
68 -- Error_Msg_AP --
69 ------------------
70
71 procedure Error_Msg_AP (Msg : String) is
72 S1 : Source_Ptr;
73 C : Character;
74
75 begin
76 -- If we had saved the Scan_Ptr value after scanning the previous
77 -- token, then we would have exactly the right place for putting
78 -- the flag immediately at hand. However, that would add at least
79 -- two instructions to a Scan call *just* to service the possibility
80 -- of an Error_Msg_AP call. So instead we reconstruct that value.
81
82 -- We have two possibilities, start with Prev_Token_Ptr and skip over
83 -- the current token, which is made harder by the possibility that this
84 -- token may be in error, or start with Token_Ptr and work backwards.
85 -- We used to take the second approach, but it's hard because of
86 -- comments, and harder still because things that look like comments
87 -- can appear inside strings. So now we take the first approach.
88
89 -- Note: in the case where there is no previous token, Prev_Token_Ptr
90 -- is set to Source_First, which is a reasonable position for the
91 -- error flag in this situation.
92
93 S1 := Prev_Token_Ptr;
94 C := Source (S1);
95
96 -- If the previous token is a string literal, we need a special approach
97 -- since there may be white space inside the literal and we don't want
98 -- to stop on that white space.
99
100 -- Note that it is not worth worrying about special UTF_32 line
101 -- terminator characters in this context, since this is only about
102 -- error recovery anyway.
103
104 if Prev_Token = Tok_String_Literal then
105 loop
106 S1 := S1 + 1;
107
108 if Source (S1) = C then
109 S1 := S1 + 1;
110 exit when Source (S1) /= C;
111 elsif Source (S1) in Line_Terminator then
112 exit;
113 end if;
114 end loop;
115
116 -- Character literal also needs special handling
117
118 elsif Prev_Token = Tok_Char_Literal then
119 S1 := S1 + 3;
120
121 -- Otherwise we search forward for the end of the current token, marked
122 -- by a line terminator, white space, a comment symbol or if we bump
123 -- into the following token (i.e. the current token)
124
125 -- Note that it is not worth worrying about special UTF_32 line
126 -- terminator characters in this context, since this is only about
127 -- error recovery anyway.
128
129 else
130 while Source (S1) not in Line_Terminator
131 and then Source (S1) /= ' '
132 and then Source (S1) /= ASCII.HT
133 and then (Source (S1) /= '-' or else Source (S1 + 1) /= '-')
134 and then S1 /= Token_Ptr
135 loop
136 S1 := S1 + 1;
137 end loop;
138 end if;
139
140 -- S1 is now set to the location for the flag
141
142 Error_Msg (Msg, S1);
143
144 end Error_Msg_AP;
145
146 ---------------
147 -- Error_Msg --
148 ---------------
149
150 procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr) is
151
152 Next_Msg : Error_Msg_Id;
153 -- Pointer to next message at insertion point
154
155 Prev_Msg : Error_Msg_Id;
156 -- Pointer to previous message at insertion point
157
158 Sptr : Source_Ptr renames Flag_Location;
159 -- Corresponds to the Sptr value in the error message object
160
161 Optr : Source_Ptr renames Flag_Location;
162 -- Corresponds to the Optr value in the error message object. Note that
163 -- for this usage, Sptr and Optr always have the same value, since we do
164 -- not have to worry about generic instantiations.
165
166 begin
167 if Errors_Must_Be_Ignored then
168 return;
169 end if;
170
171 Prescan_Message (Msg);
172 Set_Msg_Text (Msg, Sptr);
173
174 -- Kill continuation if parent message killed
175
176 if Continuation and Last_Killed then
177 return;
178 end if;
179
180 -- Return without doing anything if message is killed and this is not
181 -- the first error message. The philosophy is that if we get a weird
182 -- error message and we already have had a message, then we hope the
183 -- weird message is a junk cascaded message
184
185 -- Immediate return if warning message and warnings are suppressed.
186 -- Note that style messages are not warnings for this purpose.
187
188 if Error_Msg_Kind = Warning
189 and then Warnings_Suppressed (Sptr) /= No_String
190 then
191 Cur_Msg := No_Error_Msg;
192 return;
193 end if;
194
195 -- Otherwise build error message object for new message
196
197 Errors.Append
198 (New_Val =>
199 (Text => new String'(Msg_Buffer (1 .. Msglen)),
200 Next => No_Error_Msg,
201 Prev => No_Error_Msg,
202 Sfile => Get_Source_File_Index (Sptr),
203 Sptr => To_Span (Sptr),
204 Optr => To_Span (Optr),
205 Insertion_Sloc => No_Location,
206 Line => Get_Physical_Line_Number (Sptr),
207 Col => Get_Column_Number (Sptr),
208 Compile_Time_Pragma => Is_Compile_Time_Msg,
209 Warn_Err => Warning_Mode = Treat_As_Error,
210 Warn_Chr => Warning_Msg_Char,
211 Uncond => Is_Unconditional_Msg,
212 Msg_Cont => Continuation,
213 Deleted => False,
214 Kind => Error_Msg_Kind));
215
216 Cur_Msg := Errors.Last;
217 Prev_Msg := No_Error_Msg;
218 Next_Msg := First_Error_Msg;
219
220 while Next_Msg /= No_Error_Msg loop
221 exit when
222 Errors.Table (Cur_Msg).Sfile < Errors.Table (Next_Msg).Sfile;
223
224 if Errors.Table (Cur_Msg).Sfile = Errors.Table (Next_Msg).Sfile then
225 exit when Sptr < Errors.Table (Next_Msg).Sptr.Ptr;
226 end if;
227
228 Prev_Msg := Next_Msg;
229 Next_Msg := Errors.Table (Next_Msg).Next;
230 end loop;
231
232 -- Now we insert the new message in the error chain. The insertion
233 -- point for the message is after Prev_Msg and before Next_Msg.
234
235 -- The possible insertion point for the new message is after Prev_Msg
236 -- and before Next_Msg. However, this is where we do a special check
237 -- for redundant parsing messages, defined as messages posted on the
238 -- same line. The idea here is that probably such messages are junk
239 -- from the parser recovering. In full errors mode, we don't do this
240 -- deletion, but otherwise such messages are discarded at this stage.
241
242 if Is_Redundant_Error_Message (Prev_Msg, Cur_Msg) then
243 pragma Assert (not Continuation);
244
245 Last_Killed := True;
246 return;
247 end if;
248
249 -- Come here if message is to be inserted in the error chain
250
251 if not Continuation then
252 Last_Killed := False;
253 end if;
254
255 if Prev_Msg = No_Error_Msg then
256 First_Error_Msg := Cur_Msg;
257 else
258 Errors.Table (Prev_Msg).Next := Cur_Msg;
259 end if;
260
261 Errors.Table (Cur_Msg).Next := Next_Msg;
262
263 Increase_Error_Msg_Count (Errors.Table (Cur_Msg));
264 end Error_Msg;
265
266 -----------------
267 -- Error_Msg_S --
268 -----------------
269
270 procedure Error_Msg_S (Msg : String) is
271 begin
272 Error_Msg (Msg, Scan_Ptr);
273 end Error_Msg_S;
274
275 ------------------
276 -- Error_Msg_SC --
277 ------------------
278
279 procedure Error_Msg_SC (Msg : String) is
280 begin
281 -- If we are at end of file, post the flag after the previous token
282
283 if Token = Tok_EOF then
284 Error_Msg_AP (Msg);
285
286 -- For all other cases the message is posted at the current token
287 -- pointer position
288
289 else
290 Error_Msg (Msg, Token_Ptr);
291 end if;
292 end Error_Msg_SC;
293
294 ------------------
295 -- Error_Msg_SP --
296 ------------------
297
298 procedure Error_Msg_SP (Msg : String) is
299 begin
300 -- Note: in the case where there is no previous token, Prev_Token_Ptr
301 -- is set to Source_First, which is a reasonable position for the
302 -- error flag in this situation
303
304 Error_Msg (Msg, Prev_Token_Ptr);
305 end Error_Msg_SP;
306
307 --------------
308 -- Finalize --
309 --------------
310
311 procedure Finalize (Source_Type : String := "project") is
312 Cur : Error_Msg_Id;
313 Nxt : Error_Msg_Id;
314 E, F : Error_Msg_Id;
315 Err_Flag : Boolean;
316
317 begin
318 -- Eliminate any duplicated error messages from the list. This is
319 -- done after the fact to avoid problems with Change_Error_Text.
320
321 Cur := First_Error_Msg;
322 while Cur /= No_Error_Msg loop
323 Nxt := Errors.Table (Cur).Next;
324
325 F := Nxt;
326 while F /= No_Error_Msg
327 and then Errors.Table (F).Sptr = Errors.Table (Cur).Sptr
328 loop
329 Check_Duplicate_Message (Cur, F);
330 F := Errors.Table (F).Next;
331 end loop;
332
333 Cur := Nxt;
334 end loop;
335
336 -- Brief Error mode
337
338 if Brief_Output or (not Full_List and not Verbose_Mode) then
339 E := First_Error_Msg;
340 Set_Standard_Error;
341
342 while E /= No_Error_Msg loop
343 if not Errors.Table (E).Deleted then
344 Output_Msg_Location (E);
345 Output_Msg_Text (E);
346 Write_Eol;
347 end if;
348
349 E := Errors.Table (E).Next;
350 end loop;
351
352 Set_Standard_Output;
353 end if;
354
355 -- Full source listing case
356
357 if Full_List then
358 List_Pragmas_Index := 1;
359 List_Pragmas_Mode := True;
360 E := First_Error_Msg;
361 Write_Eol;
362
363 -- First list initial main source file with its error messages
364
365 for N in 1 .. Last_Source_Line (Main_Source_File) loop
366 Err_Flag :=
367 E /= No_Error_Msg
368 and then Errors.Table (E).Line = N
369 and then Errors.Table (E).Sfile = Main_Source_File;
370
371 Output_Source_Line (N, Main_Source_File, Err_Flag, Source_Type);
372
373 if Err_Flag then
374 Output_Error_Msgs (E);
375
376 Write_Eol;
377 end if;
378 end loop;
379
380 -- Then output errors, if any, for subsidiary units
381
382 while E /= No_Error_Msg
383 and then Errors.Table (E).Sfile /= Main_Source_File
384 loop
385 Write_Eol;
386 Output_Source_Line
387 (Errors.Table (E).Line,
388 Errors.Table (E).Sfile,
389 True,
390 Source_Type);
391 Output_Error_Msgs (E);
392 end loop;
393 end if;
394
395 -- Verbose mode (error lines only with error flags)
396
397 if Verbose_Mode then
398 E := First_Error_Msg;
399
400 -- Loop through error lines
401
402 while E /= No_Error_Msg loop
403 Write_Eol;
404 Output_Source_Line
405 (Errors.Table (E).Line,
406 Errors.Table (E).Sfile,
407 True,
408 Source_Type);
409 Output_Error_Msgs (E);
410 end loop;
411 end if;
412
413 -- Output error summary if verbose or full list mode
414
415 if Verbose_Mode or else Full_List then
416 Write_Error_Summary;
417 end if;
418
419 if Maximum_Messages /= 0 then
420 if Warnings_Detected >= Maximum_Messages then
421 Set_Standard_Error;
422 Write_Line ("maximum number of warnings detected");
423
424 Warning_Mode := Suppress;
425 end if;
426
427 if Total_Errors_Detected >= Maximum_Messages then
428 Set_Standard_Error;
429 Write_Line ("fatal error: maximum errors reached");
430 Set_Standard_Output;
431 end if;
432 end if;
433
434 -- Even though Warning_Info_Messages are a subclass of warnings, they
435 -- must not be treated as errors when -gnatwe is in effect.
436
437 if Warning_Mode = Treat_As_Error then
438 Total_Errors_Detected := Total_Errors_Detected + Warnings_Detected;
439 Warnings_Detected := 0;
440 end if;
441
442 -- Prevent displaying the same messages again in the future
443
444 First_Error_Msg := No_Error_Msg;
445 end Finalize;
446
447 ----------------
448 -- Initialize --
449 ----------------
450
451 procedure Initialize is
452 begin
453 Errors.Init;
454 First_Error_Msg := No_Error_Msg;
455 Last_Error_Msg := No_Error_Msg;
456 Serious_Errors_Detected := 0;
457 Total_Errors_Detected := 0;
458 Warnings_Detected := 0;
459 Info_Messages := 0;
460 Cur_Msg := No_Error_Msg;
461
462 -- Initialize warnings table, if all warnings are suppressed, supply
463 -- an initial dummy entry covering all possible source locations.
464
465 Warnings.Init;
466
467 if Warning_Mode = Suppress then
468 Warnings.Append
469 (New_Val =>
470 (Start => Source_Ptr'First,
471 Stop => Source_Ptr'Last,
472 Reason => Null_String_Id));
473 end if;
474 end Initialize;
475
476 ------------------------
477 -- Output_Source_Line --
478 ------------------------
479
480 procedure Output_Source_Line
481 (L : Physical_Line_Number;
482 Sfile : Source_File_Index;
483 Errs : Boolean;
484 Source_Type : String)
485 is
486 S : Source_Ptr;
487 C : Character;
488
489 Line_Number_Output : Boolean := False;
490 -- Set True once line number is output
491
492 begin
493 if Sfile /= Current_Error_Source_File then
494 Write_Str ("==============Error messages for ");
495 Write_Str (Source_Type);
496 Write_Str (" file: ");
497 Write_Name (Full_File_Name (Sfile));
498 Write_Eol;
499 Current_Error_Source_File := Sfile;
500 end if;
501
502 if Errs then
503 Output_Line_Number (Physical_To_Logical (L, Sfile));
504 Line_Number_Output := True;
505 end if;
506
507 S := Line_Start (L, Sfile);
508
509 loop
510 C := Source_Text (Sfile) (S);
511 exit when C = ASCII.LF or else C = ASCII.CR or else C = EOF;
512
513 if Errs then
514 Write_Char (C);
515 end if;
516
517 S := S + 1;
518 end loop;
519
520 if Line_Number_Output then
521 Write_Eol;
522 end if;
523 end Output_Source_Line;
524
525 -----------------------
526 -- Set_Ignore_Errors --
527 -----------------------
528
529 procedure Set_Ignore_Errors (To : Boolean) is
530 begin
531 Errors_Must_Be_Ignored := To;
532 end Set_Ignore_Errors;
533
534 ------------------
535 -- Set_Msg_Text --
536 ------------------
537
538 procedure Set_Msg_Text (Text : String; Flag : Source_Ptr) is
539 C : Character; -- Current character
540 P : Natural; -- Current index;
541
542 begin
543 Manual_Quote_Mode := False;
544 Msglen := 0;
545 Flag_Source := Get_Source_File_Index (Flag);
546 P := Text'First;
547
548 while P <= Text'Last loop
549 C := Text (P);
550 P := P + 1;
551
552 -- Check for insertion character
553
554 if C = '%' then
555 if P <= Text'Last and then Text (P) = '%' then
556 P := P + 1;
557 Set_Msg_Insertion_Name_Literal;
558 else
559 Set_Msg_Insertion_Name;
560 end if;
561
562 elsif C = '$' then
563
564 -- '$' is ignored
565
566 null;
567
568 elsif C = '{' then
569 Set_Msg_Insertion_File_Name;
570
571 elsif C = '}' then
572
573 -- '}' is ignored
574
575 null;
576
577 elsif C = '*' then
578 Set_Msg_Insertion_Reserved_Name;
579
580 elsif C = '&' then
581
582 -- '&' is ignored
583
584 null;
585
586 elsif C = '#' then
587 Set_Msg_Insertion_Line_Number (Error_Msg_Sloc, Flag);
588
589 elsif C = '\' then
590 Continuation := True;
591
592 elsif C = '@' then
593 Set_Msg_Insertion_Column;
594
595 elsif C = '^' then
596 Set_Msg_Insertion_Uint;
597
598 elsif C = '`' then
599 Manual_Quote_Mode := not Manual_Quote_Mode;
600 Set_Msg_Char ('"');
601
602 elsif C = '!' then
603 null;
604
605 elsif C = '?' then
606 null;
607
608 elsif C = '<' then
609 null;
610
611 elsif C = '|' then
612 null;
613
614 elsif C = ''' then
615 Set_Msg_Char (Text (P));
616 P := P + 1;
617
618 -- Upper case letter (start of reserved word if 2 or more)
619
620 elsif C in 'A' .. 'Z'
621 and then P <= Text'Last
622 and then Text (P) in 'A' .. 'Z'
623 then
624 P := P - 1;
625 Set_Msg_Insertion_Reserved_Word (Text, P);
626
627 elsif C = '~' then
628 Set_Msg_Str (Error_Msg_String (1 .. Error_Msg_Strlen));
629
630 -- Normal character with no special treatment
631
632 else
633 Set_Msg_Char (C);
634 end if;
635
636 end loop;
637 end Set_Msg_Text;
638
639 end Errutil;