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