Debian Codegraph @

gcc/ada/diagnostics-pretty_emitter.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- D I A G N O S T I C S . P R E T T Y _ E M I T T E R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-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 Diagnostics.Utils; use Diagnostics.Utils;
27 with Output; use Output;
28 with Sinput; use Sinput;
29 with Erroutc; use Erroutc;
30
31 package body Diagnostics.Pretty_Emitter is
32
33 REGION_OFFSET : constant := 1;
34 -- Number of characters between the line bar and the region span
35
36 REGION_ARM_SIZE : constant := 2;
37 -- Number of characters on the region span arms
38 -- e.g. two for this case:
39 -- +--
40 -- |
41 -- +--
42 -- ^^
43
44 REGION_SIZE : constant := REGION_OFFSET + 1 + REGION_ARM_SIZE;
45 -- The total number of characters taken up by the region span characters
46
47 MAX_BAR_POS : constant := 7;
48 -- The maximum position of the line bar from the start of the line
49 type Printable_Line is record
50 First : Source_Ptr;
51 -- The first character of the line
52
53 Last : Source_Ptr;
54 -- The last character of the line
55
56 Line_Nr : Pos;
57 -- The line number
58
59 Spans : Labeled_Span_List;
60 -- The spans applied on the line
61 end record;
62
63 procedure Destroy (Elem : in out Printable_Line);
64 pragma Inline (Destroy);
65
66 function Equals (L, R : Printable_Line) return Boolean is
67 (L.Line_Nr = R.Line_Nr);
68
69 package Lines_Lists is new Doubly_Linked_Lists
70 (Element_Type => Printable_Line,
71 "=" => Equals,
72 Destroy_Element => Destroy,
73 Check_Tampering => False);
74
75 subtype Lines_List is Lines_Lists.Doubly_Linked_List;
76
77 type File_Sections is record
78 File : String_Ptr;
79 -- Name of the file
80
81 Lines : Lines_List;
82 -- Lines to be printed for the file
83 end record;
84
85 procedure Destroy (Elem : in out File_Sections);
86 pragma Inline (Destroy);
87
88 function Equals (L, R : File_Sections) return Boolean is
89 (L.File /= null
90 and then R.File /= null
91 and then L.File.all = R.File.all);
92
93 package File_Section_Lists is new Doubly_Linked_Lists
94 (Element_Type => File_Sections,
95 "=" => Equals,
96 Destroy_Element => Destroy,
97 Check_Tampering => False);
98
99 subtype File_Section_List is File_Section_Lists.Doubly_Linked_List;
100
101 function Create_File_Sections (Spans : Labeled_Span_List)
102 return File_Section_List;
103 -- Create a list of file sections from the labeled spans that are to be
104 -- printed.
105 --
106 -- Each file section contains a list of lines that are to be printed for
107 -- the file and the spans that are applied to each of those lines.
108
109 procedure Create_File_Section
110 (Sections : in out File_Section_List;
111 Loc : Labeled_Span_Type);
112 -- Create a new file section for the given labeled span.
113
114 procedure Add_Printable_Line
115 (Lines : Lines_List;
116 Loc : Labeled_Span_Type;
117 S_Ptr : Source_Ptr);
118
119 procedure Create_Printable_Line
120 (Lines : Lines_List;
121 Loc : Labeled_Span_Type;
122 S_Ptr : Source_Ptr);
123 -- Create a new printable line for the given labeled span and add it in the
124 -- correct position to the Lines list based on the line number.
125
126 function Has_Region_Span_Start (L : Printable_Line) return Boolean;
127 function Has_Region_Span_End (L : Printable_Line) return Boolean;
128
129 function Has_Multiple_Labeled_Spans (L : Printable_Line) return Boolean;
130
131 procedure Write_Region_Delimiter;
132 -- Write the arms signifying the start and end of a region span
133 -- e.g. +--
134
135 procedure Write_Region_Bar;
136 -- Write the bar signifying the continuation of a region span
137 -- e.g. |
138
139 procedure Write_Region_Continuation;
140 -- Write the continuation signifying the continuation of a region span
141 -- e.g. :
142
143 procedure Write_Region_Offset;
144 -- Write a number of whitespaces equal to the size of the region span
145
146 function Trimmed_Image (I : Natural) return String;
147
148 procedure Write_Span_Labels (Loc : Labeled_Span_Type;
149 L : Printable_Line;
150 Line_Size : Integer;
151 Idx : String;
152 Within_Region_Span : Boolean);
153
154 procedure Write_File_Section (Sec : File_Sections;
155 Write_File_Name : Boolean;
156 File_Name_Offset : Integer);
157
158 procedure Write_Labeled_Spans (Spans : Labeled_Span_List;
159 Write_File_Name : Boolean;
160 File_Name_Offset : Integer);
161
162 procedure Write_Intersecting_Labels
163 (Intersecting_Labels : Labeled_Span_List);
164
165 function Get_Line_End
166 (Buf : Source_Buffer_Ptr;
167 Loc : Source_Ptr) return Source_Ptr;
168 -- Get the source location for the end of the line (LF) in Buf for Loc. If
169 -- Loc is past the end of Buf already, return Buf'Last.
170
171 function Get_Line_Start
172 (Buf : Source_Buffer_Ptr;
173 Loc : Source_Ptr) return Source_Ptr;
174 -- Get the source location for the start of the line in Buf for Loc
175
176 function Get_First_Line_Char
177 (Buf : Source_Buffer_Ptr; Loc : Source_Ptr) return Source_Ptr;
178 -- Get first non-space character in the line containing Loc
179
180 function Get_Last_Line_Char
181 (Buf : Source_Buffer_Ptr; Loc : Source_Ptr) return Source_Ptr;
182 -- Get last non line end [LF, CR] character in the line containing Loc
183
184 function Image (X : Positive; Width : Positive) return String;
185 -- Output number X over Width characters, with whitespace padding.
186 -- Only output the low-order Width digits of X, if X is larger than
187 -- Width digits.
188
189 procedure Write_Buffer
190 (Buf : Source_Buffer_Ptr;
191 First : Source_Ptr;
192 Last : Source_Ptr);
193 -- Output the characters from First to Last position in Buf, using
194 -- Write_Buffer_Char.
195
196 procedure Write_Buffer_Char
197 (Buf : Source_Buffer_Ptr;
198 Loc : Source_Ptr);
199 -- Output the characters at position Loc in Buf, translating ASCII.HT
200 -- in a suitable number of spaces so that the output is not modified
201 -- by starting in a different column that 1.
202
203 procedure Write_Line_Marker
204 (Num : Pos;
205 Width : Positive);
206
207 procedure Write_Empty_Bar_Line (Width : Integer);
208
209 procedure Write_Empty_Skip_Line (Width : Integer);
210
211 procedure Write_Error_Msg_Line (Diag : Diagnostic_Type);
212 -- Write the error message line for the given diagnostic:
213 --
214 -- '['<Diag.Id>']' <Diag.Kind>: <Diag.Message> ['['<Diag.Switch>']']
215
216 function Should_Write_File_Name (Sub_Diag : Sub_Diagnostic_Type;
217 Diag : Diagnostic_Type) return Boolean;
218 -- If the sub-diagnostic and the main diagnostic only point to the same
219 -- file then there is no reason to add the file name to the sub-diagnostic.
220
221 function Should_Write_Spans (Sub_Diag : Sub_Diagnostic_Type;
222 Diag : Diagnostic_Type)
223 return Boolean;
224 -- Old sub-diagnostics used to have the same location as the main
225 -- diagnostic in order to group them correctly. However in most cases
226 -- it was not meant to point to a location but rather add an additional
227 -- message to the original diagnostic.
228 --
229 -- If the sub-diagnostic and the main diagnostic have the same location
230 -- then we should avoid printing the spans.
231
232 procedure Print_Edit
233 (Edit : Edit_Type;
234 Offset : Integer);
235
236 procedure Print_Fix
237 (Fix : Fix_Type;
238 Offset : Integer);
239
240 procedure Print_Sub_Diagnostic
241 (Sub_Diag : Sub_Diagnostic_Type;
242 Diag : Diagnostic_Type;
243 Offset : Integer);
244
245 -------------
246 -- Destroy --
247 -------------
248
249 procedure Destroy (Elem : in out Printable_Line)
250 is
251 begin
252 -- Diagnostic elements will be freed when all the diagnostics have been
253 -- emitted.
254 null;
255 end Destroy;
256
257 -------------
258 -- Destroy --
259 -------------
260
261 procedure Destroy (Elem : in out File_Sections)
262 is
263 begin
264 Free (Elem.File);
265 end Destroy;
266
267 ------------------
268 -- Get_Line_End --
269 ------------------
270
271 function Get_Line_End
272 (Buf : Source_Buffer_Ptr; Loc : Source_Ptr) return Source_Ptr
273 is
274 Cur_Loc : Source_Ptr := Source_Ptr'Min (Loc, Buf'Last);
275 begin
276 while Cur_Loc < Buf'Last
277 and then Buf (Cur_Loc) /= ASCII.LF
278 loop
279 Cur_Loc := Cur_Loc + 1;
280 end loop;
281
282 return Cur_Loc;
283 end Get_Line_End;
284
285 --------------------
286 -- Get_Line_Start --
287 --------------------
288
289 function Get_Line_Start
290 (Buf : Source_Buffer_Ptr; Loc : Source_Ptr) return Source_Ptr
291 is
292 Cur_Loc : Source_Ptr := Loc;
293 begin
294 while Cur_Loc > Buf'First
295 and then Buf (Cur_Loc - 1) /= ASCII.LF
296 loop
297 Cur_Loc := Cur_Loc - 1;
298 end loop;
299
300 return Cur_Loc;
301 end Get_Line_Start;
302
303 -------------------------
304 -- Get_First_Line_Char --
305 -------------------------
306
307 function Get_First_Line_Char
308 (Buf : Source_Buffer_Ptr; Loc : Source_Ptr) return Source_Ptr
309 is
310 Cur_Loc : Source_Ptr := Get_Line_Start (Buf, Loc);
311 begin
312 while Cur_Loc < Buf'Last
313 and then Buf (Cur_Loc) = ' '
314 loop
315 Cur_Loc := Cur_Loc + 1;
316 end loop;
317
318 return Cur_Loc;
319 end Get_First_Line_Char;
320
321 ------------------------
322 -- Get_Last_Line_Char --
323 ------------------------
324
325 function Get_Last_Line_Char
326 (Buf : Source_Buffer_Ptr; Loc : Source_Ptr) return Source_Ptr
327 is
328 Cur_Loc : Source_Ptr := Get_Line_End (Buf, Loc);
329 begin
330 while Cur_Loc > Buf'First
331 and then Buf (Cur_Loc) in ASCII.LF | ASCII.CR
332 loop
333 Cur_Loc := Cur_Loc - 1;
334 end loop;
335
336 return Cur_Loc;
337 end Get_Last_Line_Char;
338
339 -----------
340 -- Image --
341 -----------
342
343 function Image (X : Positive; Width : Positive) return String is
344 Str : String (1 .. Width);
345 Curr : Natural := X;
346 begin
347 for J in reverse 1 .. Width loop
348 if Curr > 0 then
349 Str (J) := Character'Val (Character'Pos ('0') + Curr mod 10);
350 Curr := Curr / 10;
351 else
352 Str (J) := ' ';
353 end if;
354 end loop;
355
356 return Str;
357 end Image;
358
359 --------------------------------
360 -- Has_Multiple_Labeled_Spans --
361 --------------------------------
362
363 function Has_Multiple_Labeled_Spans (L : Printable_Line) return Boolean
364 is
365 Count : Natural := 0;
366
367 Loc : Labeled_Span_Type;
368 Loc_It : Labeled_Span_Lists.Iterator :=
369 Labeled_Span_Lists.Iterate (L.Spans);
370 begin
371 while Labeled_Span_Lists.Has_Next (Loc_It) loop
372 Labeled_Span_Lists.Next (Loc_It, Loc);
373 if Loc.Label /= null then
374 Count := Count + 1;
375 end if;
376 end loop;
377
378 return Count > 1;
379 end Has_Multiple_Labeled_Spans;
380
381 ---------------------------
382 -- Has_Region_Span_Start --
383 ---------------------------
384
385 function Has_Region_Span_Start (L : Printable_Line) return Boolean is
386 Loc : Labeled_Span_Type;
387 Loc_It : Labeled_Span_Lists.Iterator :=
388 Labeled_Span_Lists.Iterate (L.Spans);
389
390 Has_Region_Start : Boolean := False;
391 begin
392 while Labeled_Span_Lists.Has_Next (Loc_It) loop
393 Labeled_Span_Lists.Next (Loc_It, Loc);
394
395 if not Has_Region_Start
396 and then Loc.Is_Region
397 and then L.Line_Nr =
398 Pos (Get_Physical_Line_Number (Loc.Span.First))
399 then
400 Has_Region_Start := True;
401 end if;
402 end loop;
403 return Has_Region_Start;
404 end Has_Region_Span_Start;
405
406 -------------------------
407 -- Has_Region_Span_End --
408 -------------------------
409
410 function Has_Region_Span_End (L : Printable_Line) return Boolean is
411 Loc : Labeled_Span_Type;
412 Loc_It : Labeled_Span_Lists.Iterator :=
413 Labeled_Span_Lists.Iterate (L.Spans);
414
415 Has_Region_End : Boolean := False;
416 begin
417 while Labeled_Span_Lists.Has_Next (Loc_It) loop
418 Labeled_Span_Lists.Next (Loc_It, Loc);
419
420 if not Has_Region_End
421 and then Loc.Is_Region
422 and then L.Line_Nr =
423 Pos (Get_Physical_Line_Number (Loc.Span.Last))
424 then
425 Has_Region_End := True;
426 end if;
427 end loop;
428 return Has_Region_End;
429 end Has_Region_Span_End;
430
431 ------------------
432 -- Write_Buffer --
433 ------------------
434
435 procedure Write_Buffer
436 (Buf : Source_Buffer_Ptr;
437 First : Source_Ptr;
438 Last : Source_Ptr)
439 is
440 begin
441 for Loc in First .. Last loop
442 Write_Buffer_Char (Buf, Loc);
443 end loop;
444 end Write_Buffer;
445
446 -----------------------
447 -- Write_Buffer_Char --
448 -----------------------
449
450 procedure Write_Buffer_Char
451 (Buf : Source_Buffer_Ptr;
452 Loc : Source_Ptr)
453 is
454 begin
455 -- If the character ASCII.HT is not the last one in the file,
456 -- output as many spaces as the character represents in the
457 -- original source file.
458
459 if Buf (Loc) = ASCII.HT
460 and then Loc < Buf'Last
461 then
462 for X in Get_Column_Number (Loc) ..
463 Get_Column_Number (Loc + 1) - 1
464 loop
465 Write_Char (' ');
466 end loop;
467
468 -- Otherwise output the character itself
469
470 else
471 Write_Char (Buf (Loc));
472 end if;
473 end Write_Buffer_Char;
474
475 -----------------------
476 -- Write_Line_Marker --
477 -----------------------
478
479 procedure Write_Line_Marker
480 (Num : Pos;
481 Width : Positive)
482 is
483 begin
484 Write_Str (Image (Positive (Num), Width => Width - 2));
485 Write_Str (" |");
486 end Write_Line_Marker;
487
488 --------------------------
489 -- Write_Empty_Bar_Line --
490 --------------------------
491
492 procedure Write_Empty_Bar_Line (Width : Integer) is
493
494 begin
495 Write_Str (String'(1 .. Width - 1 => ' '));
496 Write_Str ("|");
497 end Write_Empty_Bar_Line;
498
499 ---------------------------
500 -- Write_Empty_Skip_Line --
501 ---------------------------
502
503 procedure Write_Empty_Skip_Line (Width : Integer) is
504
505 begin
506 Write_Str (String'(1 .. Width - 1 => ' '));
507 Write_Str (":");
508 end Write_Empty_Skip_Line;
509
510 ----------------------------
511 -- Write_Region_Delimiter --
512 ----------------------------
513
514 procedure Write_Region_Delimiter is
515
516 begin
517 Write_Str (String'(1 .. REGION_OFFSET => ' '));
518 Write_Str ("+");
519 Write_Str (String'(1 .. REGION_ARM_SIZE => '-'));
520 end Write_Region_Delimiter;
521
522 ----------------------
523 -- Write_Region_Bar --
524 ----------------------
525
526 procedure Write_Region_Bar is
527
528 begin
529 Write_Str (String'(1 .. REGION_OFFSET => ' '));
530 Write_Str ("|");
531 Write_Str (String'(1 .. REGION_ARM_SIZE => ' '));
532 end Write_Region_Bar;
533
534 -------------------------------
535 -- Write_Region_Continuation --
536 -------------------------------
537
538 procedure Write_Region_Continuation is
539
540 begin
541 Write_Str (String'(1 .. REGION_OFFSET => ' '));
542 Write_Str (":");
543 Write_Str (String'(1 .. REGION_ARM_SIZE => ' '));
544 end Write_Region_Continuation;
545
546 -------------------------
547 -- Write_Region_Offset --
548 -------------------------
549
550 procedure Write_Region_Offset is
551
552 begin
553 Write_Str (String'(1 .. REGION_SIZE => ' '));
554 end Write_Region_Offset;
555
556 ------------------------
557 -- Add_Printable_Line --
558 ------------------------
559
560 procedure Add_Printable_Line
561 (Lines : Lines_List;
562 Loc : Labeled_Span_Type;
563 S_Ptr : Source_Ptr)
564 is
565 L : Printable_Line;
566 L_It : Lines_Lists.Iterator;
567
568 Line_Ptr : constant Pos := Pos (Get_Physical_Line_Number (S_Ptr));
569 Line_Found : Boolean := False;
570 begin
571 L_It := Lines_Lists.Iterate (Lines);
572 while Lines_Lists.Has_Next (L_It) loop
573 Lines_Lists.Next (L_It, L);
574
575 if not Line_Found and then L.Line_Nr = Line_Ptr then
576 if not Labeled_Span_Lists.Contains (L.Spans, Loc) then
577 Labeled_Span_Lists.Append (L.Spans, Loc);
578 end if;
579 Line_Found := True;
580 end if;
581 end loop;
582
583 if not Line_Found then
584 Create_Printable_Line (Lines, Loc, S_Ptr);
585 end if;
586 end Add_Printable_Line;
587
588 ---------------------------
589 -- Create_Printable_Line --
590 ---------------------------
591
592 procedure Create_Printable_Line
593 (Lines : Lines_List;
594 Loc : Labeled_Span_Type;
595 S_Ptr : Source_Ptr)
596 is
597 Spans : constant Labeled_Span_List := Labeled_Span_Lists.Create;
598
599 Buf : constant Source_Buffer_Ptr :=
600 Source_Text (Get_Source_File_Index (S_Ptr));
601
602 Line_Nr : constant Pos := Pos (Get_Physical_Line_Number (S_Ptr));
603
604 New_Line : constant Printable_Line :=
605 (First => Get_Line_Start (Buf, S_Ptr),
606 Last => Get_Line_End (Buf, S_Ptr),
607 Line_Nr => Line_Nr,
608 Spans => Spans);
609
610 L : Printable_Line;
611 L_It : Lines_Lists.Iterator := Lines_Lists.Iterate (Lines);
612
613 Found_Greater_Line : Boolean := False;
614 Insert_Before_Line : Printable_Line;
615 begin
616 Labeled_Span_Lists.Append (Spans, Loc);
617
618 -- Insert the new line based on the line number
619
620 while Lines_Lists.Has_Next (L_It) loop
621 Lines_Lists.Next (L_It, L);
622
623 if not Found_Greater_Line
624 and then L.Line_Nr > New_Line.Line_Nr
625 then
626 Found_Greater_Line := True;
627 Insert_Before_Line := L;
628
629 Lines_Lists.Insert_Before (Lines, Insert_Before_Line, New_Line);
630 end if;
631 end loop;
632
633 if Found_Greater_Line then
634
635 -- Insert after all the lines have been iterated over to avoid the
636 -- mutation lock in GNAT.Lists
637
638 null;
639 else
640 Lines_Lists.Append (Lines, New_Line);
641 end if;
642 end Create_Printable_Line;
643
644 -------------------------
645 -- Create_File_Section --
646 -------------------------
647
648 procedure Create_File_Section
649 (Sections : in out File_Section_List; Loc : Labeled_Span_Type)
650 is
651 Lines : constant Lines_List := Lines_Lists.Create;
652
653 -- Carret positions
654 Ptr : constant Source_Ptr := Loc.Span.Ptr;
655 Line_Ptr : constant Pos := Pos (Get_Physical_Line_Number (Ptr));
656
657 -- Span start positions
658 Fst : constant Source_Ptr := Loc.Span.First;
659 Line_Fst : constant Pos := Pos (Get_Physical_Line_Number (Fst));
660
661 -- Span end positions
662 Lst : constant Source_Ptr := Loc.Span.Last;
663 Line_Lst : constant Pos := Pos (Get_Physical_Line_Number (Lst));
664 begin
665 Create_Printable_Line (Lines, Loc, Fst);
666
667 if Line_Fst /= Line_Ptr then
668 Create_Printable_Line (Lines, Loc, Ptr);
669 end if;
670
671 if Line_Ptr /= Line_Lst then
672 Create_Printable_Line (Lines, Loc, Lst);
673 end if;
674
675 File_Section_Lists.Append
676 (Sections,
677 (File => new String'(To_File_Name (Loc.Span.Ptr)),
678 Lines => Lines));
679 end Create_File_Section;
680
681 --------------------------
682 -- Create_File_Sections --
683 --------------------------
684
685 function Create_File_Sections
686 (Spans : Labeled_Span_List) return File_Section_List
687 is
688 Loc : Labeled_Span_Type;
689 Loc_It : Labeled_Span_Lists.Iterator :=
690 Labeled_Span_Lists.Iterate (Spans);
691
692 Sections : File_Section_List := File_Section_Lists.Create;
693
694 Sec : File_Sections;
695 F_It : File_Section_Lists.Iterator;
696
697 File_Found : Boolean;
698 begin
699 while Labeled_Span_Lists.Has_Next (Loc_It) loop
700 Labeled_Span_Lists.Next (Loc_It, Loc);
701
702 File_Found := False;
703 F_It := File_Section_Lists.Iterate (Sections);
704
705 while File_Section_Lists.Has_Next (F_It) loop
706 File_Section_Lists.Next (F_It, Sec);
707
708 if Sec.File /= null
709 and then Sec.File.all = To_File_Name (Loc.Span.Ptr)
710 then
711 File_Found := True;
712
713 Add_Printable_Line (Sec.Lines, Loc, Loc.Span.First);
714
715 Add_Printable_Line (Sec.Lines, Loc, Loc.Span.Ptr);
716
717 Add_Printable_Line (Sec.Lines, Loc, Loc.Span.Last);
718 end if;
719 end loop;
720
721 if not File_Found then
722 Create_File_Section (Sections, Loc);
723 end if;
724 end loop;
725
726 return Sections;
727 end Create_File_Sections;
728
729 -----------------------
730 -- Write_Span_Labels --
731 -----------------------
732
733 procedure Write_Span_Labels (Loc : Labeled_Span_Type;
734 L : Printable_Line;
735 Line_Size : Integer;
736 Idx : String;
737 Within_Region_Span : Boolean)
738 is
739 Span_Char : constant Character := (if Loc.Is_Primary then '~' else '-');
740
741 Buf : constant Source_Buffer_Ptr :=
742 Source_Text (Get_Source_File_Index (L.First));
743
744 Col_L_Fst : constant Natural := Natural
745 (Get_Column_Number (Get_First_Line_Char (Buf, L.First)));
746 Col_L_Lst : constant Natural := Natural
747 (Get_Column_Number (Get_Last_Line_Char (Buf, L.Last)));
748
749 -- Carret positions
750 Ptr : constant Source_Ptr := Loc.Span.Ptr;
751 Line_Ptr : constant Pos := Pos (Get_Physical_Line_Number (Ptr));
752 Col_Ptr : constant Natural := Natural (Get_Column_Number (Ptr));
753
754 -- Span start positions
755 Fst : constant Source_Ptr := Loc.Span.First;
756 Line_Fst : constant Pos := Pos (Get_Physical_Line_Number (Fst));
757 Col_Fst : constant Natural := Natural (Get_Column_Number (Fst));
758
759 -- Span end positions
760 Lst : constant Source_Ptr := Loc.Span.Last;
761 Line_Lst : constant Pos := Pos (Get_Physical_Line_Number (Lst));
762 Col_Lst : constant Natural := Natural (Get_Column_Number (Lst));
763
764 -- Attributes for the span on the current line
765
766 Span_Sym : constant String := (if Idx = "" then "^" else Idx);
767
768 Span_Fst : constant Natural :=
769 (if Line_Fst = L.Line_Nr then Col_Fst else Col_L_Fst);
770
771 Span_Lst : constant Natural :=
772 (if Line_Lst = L.Line_Nr then Col_Lst else Col_L_Lst);
773
774 Span_Ptr_Fst : constant Natural :=
775 (if Line_Ptr = L.Line_Nr then Col_Ptr else Col_L_Fst);
776
777 Span_Ptr_Lst : constant Natural :=
778 (if Line_Ptr = L.Line_Nr
779 then Span_Ptr_Fst + Span_Sym'Length
780 else Span_Fst);
781
782 begin
783 if not Loc.Is_Region then
784 Write_Empty_Bar_Line (Line_Size);
785
786 if Within_Region_Span then
787 Write_Region_Bar;
788 else
789 Write_Region_Offset;
790 end if;
791
792 Write_Str (String'(1 .. Span_Fst - 1 => ' '));
793
794 if Line_Ptr = L.Line_Nr then
795 Write_Str (String'(Span_Fst .. Col_Ptr - 1 => Span_Char));
796 Write_Str (Span_Sym);
797 end if;
798
799 Write_Str (String'(Span_Ptr_Lst .. Span_Lst => Span_Char));
800
801 Write_Eol;
802
803 -- Write the label under the line unless it is an intersecting span.
804 -- In this case omit the label which will be printed later along with
805 -- the index.
806
807 if Loc.Label /= null and then Idx = "" then
808 Write_Empty_Bar_Line (Line_Size);
809
810 if Within_Region_Span then
811 Write_Region_Bar;
812 else
813 Write_Region_Offset;
814 end if;
815
816 Write_Str (String'(1 .. Span_Fst - 1 => ' '));
817 Write_Str (Loc.Label.all);
818 Write_Eol;
819 end if;
820 else
821 if Line_Lst = L.Line_Nr then
822 Write_Empty_Bar_Line (Line_Size);
823 Write_Str (String'(1 .. REGION_OFFSET => ' '));
824 Write_Str (Loc.Label.all);
825 Write_Eol;
826 end if;
827 end if;
828
829 end Write_Span_Labels;
830
831 -------------------
832 -- Trimmed_Image --
833 -------------------
834
835 function Trimmed_Image (I : Natural) return String is
836 Img_Raw : constant String := Natural'Image (I);
837 begin
838 return Img_Raw (Img_Raw'First + 1 .. Img_Raw'Last);
839 end Trimmed_Image;
840
841 -------------------------------
842 -- Write_Intersecting_Labels --
843 -------------------------------
844
845 procedure Write_Intersecting_Labels
846 (Intersecting_Labels : Labeled_Span_List)
847 is
848 Ls : Labeled_Span_Type;
849 Ls_It : Labeled_Span_Lists.Iterator :=
850 Labeled_Span_Lists.Iterate (Intersecting_Labels);
851 Idx : Integer := 0;
852 begin
853 while Labeled_Span_Lists.Has_Next (Ls_It) loop
854 Labeled_Span_Lists.Next (Ls_It, Ls);
855 Idx := Idx + 1;
856
857 Write_Empty_Bar_Line (MAX_BAR_POS);
858 Write_Str (" ");
859 Write_Int (Int (Idx));
860 Write_Str (": ");
861 Write_Str (Ls.Label.all);
862 Write_Eol;
863 end loop;
864 end Write_Intersecting_Labels;
865
866 ------------------------
867 -- Write_File_Section --
868 ------------------------
869
870 procedure Write_File_Section (Sec : File_Sections;
871 Write_File_Name : Boolean;
872 File_Name_Offset : Integer)
873 is
874 use Lines_Lists;
875
876 L : Printable_Line;
877 L_It : Iterator := Iterate (Sec.Lines);
878
879 -- The error should be included in the first (primary) span of the file.
880 Loc : constant Labeled_Span_Type :=
881 Labeled_Span_Lists.First (Lines_Lists.First (Sec.Lines).Spans);
882
883 Multiple_Labeled_Spans : Boolean := False;
884
885 Idx : Integer := 0;
886
887 Intersecting_Labels : constant Labeled_Span_List :=
888 Labeled_Span_Lists.Create;
889
890 Prev_Line_Nr : Natural := 0;
891
892 Within_Region_Span : Boolean := False;
893 begin
894 if Write_File_Name then
895
896 -- offset the file start location for sub-diagnostics
897
898 Write_Str (String'(1 .. File_Name_Offset => ' '));
899 Write_Str ("--> " & To_String (Loc.Span.Ptr));
900 Write_Eol;
901 end if;
902
903 while Has_Next (L_It) loop
904 Next (L_It, L);
905 declare
906 Line_Nr : constant Pos := L.Line_Nr;
907 Line_Str : constant String := Trimmed_Image (Natural (Line_Nr));
908
909 Line_Size : constant Integer :=
910 Integer'Max (Line_Str'Length, MAX_BAR_POS);
911
912 Loc : Labeled_Span_Type;
913 Loc_It : Labeled_Span_Lists.Iterator :=
914 Labeled_Span_Lists.Iterate (L.Spans);
915
916 Buf : constant Source_Buffer_Ptr :=
917 Source_Text (Get_Source_File_Index (L.First));
918
919 Contains_Region_Span_Start : constant Boolean :=
920 Has_Region_Span_Start (L);
921 Contains_Region_Span_End : constant Boolean :=
922 Has_Region_Span_End (L);
923 begin
924 if not Multiple_Labeled_Spans then
925 Multiple_Labeled_Spans := Has_Multiple_Labeled_Spans (L);
926 end if;
927
928 -- Write an empty line with the continuation symbol if the line
929 -- numbers are not contiguous
930
931 if Prev_Line_Nr /= 0
932 and then Pos (Prev_Line_Nr + 1) /= Line_Nr
933 then
934 Write_Empty_Skip_Line (Line_Size);
935
936 if Within_Region_Span then
937 Write_Region_Continuation;
938 end if;
939
940 Write_Eol;
941 end if;
942
943 if Contains_Region_Span_Start then
944 Within_Region_Span := True;
945 end if;
946
947 Write_Line_Marker (Line_Nr, Line_Size);
948
949 -- Write either the region span symbol or the same number of
950 -- whitespaces.
951
952 if Contains_Region_Span_Start or Contains_Region_Span_End then
953 Write_Region_Delimiter;
954 elsif Within_Region_Span then
955 Write_Region_Bar;
956 else
957 Write_Region_Offset;
958 end if;
959
960 -- Write the line itself
961
962 Write_Buffer
963 (Buf => Buf,
964 First => L.First,
965 Last => L.Last);
966
967 -- Write all the spans for the line
968
969 while Labeled_Span_Lists.Has_Next (Loc_It) loop
970 Labeled_Span_Lists.Next (Loc_It, Loc);
971
972 if Multiple_Labeled_Spans
973 and then Loc.Label /= null
974 then
975
976 -- Collect all the spans with labels to print them at the
977 -- end.
978
979 Labeled_Span_Lists.Append (Intersecting_Labels, Loc);
980
981 Idx := Idx + 1;
982
983 Write_Span_Labels (Loc,
984 L,
985 Line_Size,
986 Trimmed_Image (Idx),
987 Within_Region_Span);
988 else
989 Write_Span_Labels (Loc,
990 L,
991 Line_Size,
992 "",
993 Within_Region_Span);
994 end if;
995
996 end loop;
997
998 if Contains_Region_Span_End then
999 Within_Region_Span := False;
1000 end if;
1001
1002 Prev_Line_Nr := Natural (Line_Nr);
1003 end;
1004 end loop;
1005
1006 Write_Intersecting_Labels (Intersecting_Labels);
1007 end Write_File_Section;
1008
1009 -------------------------
1010 -- Write_Labeled_Spans --
1011 -------------------------
1012
1013 procedure Write_Labeled_Spans (Spans : Labeled_Span_List;
1014 Write_File_Name : Boolean;
1015 File_Name_Offset : Integer)
1016 is
1017 Sections : File_Section_List := Create_File_Sections (Spans);
1018
1019 Sec : File_Sections;
1020 F_It : File_Section_Lists.Iterator :=
1021 File_Section_Lists.Iterate (Sections);
1022 begin
1023 while File_Section_Lists.Has_Next (F_It) loop
1024 File_Section_Lists.Next (F_It, Sec);
1025
1026 Write_File_Section
1027 (Sec, Write_File_Name, File_Name_Offset);
1028 end loop;
1029
1030 File_Section_Lists.Destroy (Sections);
1031 end Write_Labeled_Spans;
1032
1033 --------------------------
1034 -- Write_Error_Msg_Line --
1035 --------------------------
1036
1037 procedure Write_Error_Msg_Line (Diag : Diagnostic_Type) is
1038 Switch_Str : constant String := Get_Doc_Switch (Diag);
1039
1040 Kind_Str : constant String := Kind_To_String (Diag);
1041
1042 SGR_Code : constant String :=
1043 (if Kind_Str = "error" then SGR_Error
1044 elsif Kind_Str = "warning" then SGR_Warning
1045 elsif Kind_Str = "info" then SGR_Note
1046 else SGR_Reset);
1047 begin
1048 Write_Str (SGR_Code);
1049
1050 Write_Str ("[" & To_String (Diag.Id) & "]");
1051
1052 Write_Str (" " & Kind_To_String (Diag) & ": ");
1053
1054 Write_Str (SGR_Reset);
1055
1056 Write_Str (Diag.Message.all);
1057
1058 if Switch_Str /= "" then
1059 Write_Str (" " & Switch_Str);
1060 end if;
1061
1062 if Diag.Warn_Err then
1063 Write_Str (" [warning-as-error]");
1064 end if;
1065
1066 Write_Eol;
1067 end Write_Error_Msg_Line;
1068
1069 ----------------------------
1070 -- Should_Write_File_Name --
1071 ----------------------------
1072
1073 function Should_Write_File_Name (Sub_Diag : Sub_Diagnostic_Type;
1074 Diag : Diagnostic_Type)
1075 return Boolean
1076 is
1077 Sub_Loc : constant Labeled_Span_Type := Primary_Location (Sub_Diag);
1078 Diag_Loc : constant Labeled_Span_Type := Primary_Location (Diag);
1079
1080 function Has_Multiple_Files (Spans : Labeled_Span_List) return Boolean;
1081
1082 ------------------------
1083 -- Has_Multiple_Files --
1084 ------------------------
1085
1086 function Has_Multiple_Files
1087 (Spans : Labeled_Span_List) return Boolean
1088 is
1089 First : constant Labeled_Span_Type :=
1090 Labeled_Span_Lists.First (Spans);
1091
1092 File : constant String := To_File_Name (First.Span.Ptr);
1093
1094 Loc : Labeled_Span_Type;
1095 It : Labeled_Span_Lists.Iterator :=
1096 Labeled_Span_Lists.Iterate (Spans);
1097
1098 begin
1099 while Labeled_Span_Lists.Has_Next (It) loop
1100 Labeled_Span_Lists.Next (It, Loc);
1101
1102 if To_File_Name (Loc.Span.Ptr) /= File then
1103 return True;
1104 end if;
1105 end loop;
1106 return False;
1107 end Has_Multiple_Files;
1108 begin
1109 return
1110 Has_Multiple_Files (Diag.Locations)
1111 or else To_File_Name (Sub_Loc.Span.Ptr) /=
1112 To_File_Name (Diag_Loc.Span.Ptr);
1113 end Should_Write_File_Name;
1114
1115 ------------------------
1116 -- Should_Write_Spans --
1117 ------------------------
1118
1119 function Should_Write_Spans (Sub_Diag : Sub_Diagnostic_Type;
1120 Diag : Diagnostic_Type)
1121 return Boolean
1122 is
1123 Sub_Loc : constant Labeled_Span_Type := Primary_Location (Sub_Diag);
1124 Diag_Loc : constant Labeled_Span_Type := Primary_Location (Diag);
1125 begin
1126 return Sub_Loc /= No_Labeled_Span
1127 and then Diag_Loc /= No_Labeled_Span
1128 and then Sub_Loc.Span.Ptr /= Diag_Loc.Span.Ptr;
1129 end Should_Write_Spans;
1130
1131 ----------------
1132 -- Print_Edit --
1133 ----------------
1134
1135 procedure Print_Edit (Edit : Edit_Type; Offset : Integer) is
1136 Buf : constant Source_Buffer_Ptr :=
1137 Source_Text (Get_Source_File_Index (Edit.Span.Ptr));
1138
1139 Line_Nr : constant Pos := Pos (Get_Physical_Line_Number (Edit.Span.Ptr));
1140
1141 Line_Fst : constant Source_Ptr := Get_Line_Start (Buf, Edit.Span.First);
1142 Line_Lst : constant Source_Ptr := Get_Line_End (Buf, Edit.Span.First);
1143 begin
1144 Write_Str (String'(1 .. Offset => ' '));
1145 Write_Str ("--> " & To_File_Name (Edit.Span.Ptr));
1146 Write_Eol;
1147
1148 -- write the original line
1149
1150 Write_Char ('-');
1151 Write_Line_Marker (Line_Nr, MAX_BAR_POS - 1);
1152
1153 Write_Buffer
1154 (Buf => Buf,
1155 First => Line_Fst,
1156 Last => Line_Lst);
1157
1158 -- write the edited line
1159
1160 Write_Char ('+');
1161 Write_Line_Marker (Line_Nr, MAX_BAR_POS - 1);
1162
1163 Write_Buffer
1164 (Buf => Buf,
1165 First => Line_Fst,
1166 Last => Edit.Span.First - 1);
1167
1168 if Edit.Text /= null then
1169 Write_Str (Edit.Text.all);
1170 end if;
1171
1172 Write_Buffer
1173 (Buf => Buf,
1174 First => Edit.Span.Last + 1,
1175 Last => Line_Lst);
1176
1177 end Print_Edit;
1178
1179 ---------------
1180 -- Print_Fix --
1181 ---------------
1182
1183 procedure Print_Fix (Fix : Fix_Type; Offset : Integer) is
1184 use Edit_Lists;
1185 begin
1186 Write_Str (String'(1 .. Offset => ' '));
1187 Write_Str ("+ Fix: ");
1188
1189 if Fix.Description /= null then
1190 Write_Str (Fix.Description.all);
1191 end if;
1192 Write_Eol;
1193
1194 if Present (Fix.Edits) then
1195 declare
1196 Edit : Edit_Type;
1197
1198 It : Iterator := Iterate (Fix.Edits);
1199 begin
1200 while Has_Next (It) loop
1201 Next (It, Edit);
1202
1203 Print_Edit (Edit, MAX_BAR_POS - 1);
1204 end loop;
1205 end;
1206 end if;
1207 end Print_Fix;
1208
1209 --------------------------
1210 -- Print_Sub_Diagnostic --
1211 --------------------------
1212
1213 procedure Print_Sub_Diagnostic
1214 (Sub_Diag : Sub_Diagnostic_Type;
1215 Diag : Diagnostic_Type;
1216 Offset : Integer)
1217 is
1218 begin
1219 Write_Str (String'(1 .. Offset => ' '));
1220
1221 if Sub_Diag.Kind = Suggestion then
1222 Write_Str ("+ Suggestion: ");
1223 else
1224 Write_Str ("+ ");
1225 end if;
1226
1227 Write_Str (Sub_Diag.Message.all);
1228 Write_Eol;
1229
1230 if Should_Write_Spans (Sub_Diag, Diag) then
1231 Write_Labeled_Spans (Sub_Diag.Locations,
1232 Should_Write_File_Name (Sub_Diag, Diag),
1233 Offset);
1234 end if;
1235 end Print_Sub_Diagnostic;
1236
1237 ----------------------
1238 -- Print_Diagnostic --
1239 ----------------------
1240
1241 procedure Print_Diagnostic (Diag : Diagnostic_Type) is
1242
1243 begin
1244 -- Print the main diagnostic
1245
1246 Write_Error_Msg_Line (Diag);
1247
1248 -- Print diagnostic locations along with spans
1249
1250 Write_Labeled_Spans (Diag.Locations, True, 0);
1251
1252 -- Print subdiagnostics
1253
1254 if Sub_Diagnostic_Lists.Present (Diag.Sub_Diagnostics) then
1255 declare
1256 use Sub_Diagnostic_Lists;
1257 Sub_Diag : Sub_Diagnostic_Type;
1258
1259 It : Iterator := Iterate (Diag.Sub_Diagnostics);
1260 begin
1261 while Has_Next (It) loop
1262 Next (It, Sub_Diag);
1263
1264 -- Print the subdiagnostic and offset the location of the file
1265 -- name
1266
1267 Print_Sub_Diagnostic (Sub_Diag, Diag, MAX_BAR_POS - 1);
1268 end loop;
1269 end;
1270 end if;
1271
1272 -- Print fixes
1273
1274 if Fix_Lists.Present (Diag.Fixes) then
1275 declare
1276 use Fix_Lists;
1277 Fix : Fix_Type;
1278
1279 It : Iterator := Iterate (Diag.Fixes);
1280 begin
1281 while Has_Next (It) loop
1282 Next (It, Fix);
1283
1284 Print_Fix (Fix, MAX_BAR_POS - 1);
1285 end loop;
1286 end;
1287 end if;
1288
1289 -- Separate main diagnostics with a blank line
1290
1291 Write_Eol;
1292
1293 end Print_Diagnostic;
1294 end Diagnostics.Pretty_Emitter;