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
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- E R R O U T C . S A R I F _ E M I T T E R --
-- --
-- B o d y --
-- --
-- Copyright (C) 1992-2025, 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 JSON_Utils; use JSON_Utils;
with GNAT.Lists; use GNAT.Lists;
with Gnatvsn; use Gnatvsn;
with Lib; use Lib;
with Namet; use Namet;
with Output; use Output;
with Sinput; use Sinput;
with System.OS_Lib;
package body Erroutc.SARIF_Emitter is
-- SARIF attribute names
N_ARTIFACT_CHANGES : constant String := "artifactChanges";
N_ARTIFACT_LOCATION : constant String := "artifactLocation";
N_COMMAND_LINE : constant String := "commandLine";
N_DELETED_REGION : constant String := "deletedRegion";
N_DESCRIPTION : constant String := "description";
N_DRIVER : constant String := "driver";
N_END_COLUMN : constant String := "endColumn";
N_END_LINE : constant String := "endLine";
N_EXECUTION_SUCCESSFUL : constant String := "executionSuccessful";
N_FIXES : constant String := "fixes";
N_ID : constant String := "id";
N_INSERTED_CONTENT : constant String := "insertedContent";
N_INVOCATIONS : constant String := "invocations";
N_LOCATIONS : constant String := "locations";
N_LEVEL : constant String := "level";
N_MESSAGE : constant String := "message";
N_NAME : constant String := "name";
N_ORIGINAL_URI_BASE_IDS : constant String := "originalUriBaseIds";
N_PHYSICAL_LOCATION : constant String := "physicalLocation";
N_REGION : constant String := "region";
N_RELATED_LOCATIONS : constant String := "relatedLocations";
N_REPLACEMENTS : constant String := "replacements";
N_RESULTS : constant String := "results";
N_RULES : constant String := "rules";
N_RULE_ID : constant String := "ruleId";
N_RUNS : constant String := "runs";
N_SCHEMA : constant String := "$schema";
N_START_COLUMN : constant String := "startColumn";
N_START_LINE : constant String := "startLine";
N_TEXT : constant String := "text";
N_TOOL : constant String := "tool";
N_URI : constant String := "uri";
N_URI_BASE_ID : constant String := "uriBaseId";
N_VERSION : constant String := "version";
-- We are currently using SARIF 2.1.0
SARIF_Version : constant String := "2.1.0";
pragma Style_Checks ("M100");
SARIF_Schema : constant String :=
"https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json";
pragma Style_Checks ("M79");
URI_Base_Id_Name : constant String := "PWD";
-- We use the pwd as the originalUriBaseIds when providing absolute paths
-- in locations.
Current_Dir : constant String := Get_Current_Dir;
-- Cached value of the current directory that is used in the URI_Base_Id
-- and it is also the path that all other Uri attributes will be created
-- relative to.
procedure Destroy (Elem : in out Error_Msg_Object) is null;
pragma Inline (Destroy);
package Error_Msg_Lists is new Doubly_Linked_Lists
(Element_Type => Error_Msg_Object,
"=" => "=",
Destroy_Element => Destroy,
Check_Tampering => False);
subtype Error_Msg_List is Error_Msg_Lists.Doubly_Linked_List;
procedure Destroy (Elem : in out Edit_Type);
procedure Destroy (Elem : in out Edit_Type) is
begin
-- Diagnostic elements will be freed when all the diagnostics have been
-- emitted.
null;
end Destroy;
pragma Inline (Destroy);
package Edit_Lists is new Doubly_Linked_Lists
(Element_Type => Edit_Type,
"=" => "=",
Destroy_Element => Destroy,
Check_Tampering => False);
subtype Edit_List is Edit_Lists.Doubly_Linked_List;
type Artifact_Change is record
File_Index : Source_File_Index;
-- Index for the source file
Replacements : Edit_List;
-- Regions of texts to be edited
end record;
procedure Destroy (Elem : in out Artifact_Change);
pragma Inline (Destroy);
function Equals (L, R : Artifact_Change) return Boolean is
(L.File_Index = R.File_Index);
package Artifact_Change_Lists is new Doubly_Linked_Lists
(Element_Type => Artifact_Change,
"=" => Equals,
Destroy_Element => Destroy,
Check_Tampering => False);
subtype Artifact_Change_List is Artifact_Change_Lists.Doubly_Linked_List;
function Get_Artifact_Changes (Fix : Fix_Type) return Artifact_Change_List;
-- Group edits of a Fix into Artifact_Changes that organize the edits by
-- file name.
function Get_Unique_Rules return Error_Msg_List;
-- Get a list of diagnostics that have unique Diagnostic Id-s.
procedure Print_Replacement (Replacement : Edit_Type);
-- Print a replacement node
--
-- {
-- deletedRegion: {<Region>},
-- insertedContent: {<Message>}
-- }
procedure Print_Fix (Fix : Fix_Type);
-- Print the fix node
--
-- {
-- description: {<Message>},
-- artifactChanges: [<ArtifactChange>]
-- }
procedure Print_Fixes (E_Msg : Error_Msg_Object);
-- Print the fixes node
--
-- "fixes": [
-- <Fix>,
-- ...
-- ]
procedure Print_Invocations;
-- Print an invocations node that consists of
-- * a single invocation node that consists of:
-- * commandLine
-- * executionSuccessful
--
-- "invocations": [
-- {
-- "commandLine": <command line arguments provided to the GNAT FE>,
-- "executionSuccessful": ["true"|"false"],
-- }
-- ]
procedure Print_Artifact_Change (A : Artifact_Change);
-- Print an ArtifactChange node
--
-- {
-- artifactLocation: {<ArtifactLocation>},
-- replacements: [<Replacements>]
-- }
procedure Print_Artifact_Location (Sfile : Source_File_Index);
-- Print an artifactLocation node
--
-- "artifactLocation": {
-- "uri": <File_Name>,
-- "uriBaseId": "PWD"
-- }
procedure Print_Location (Loc : Labeled_Span_Type; Msg : String_Ptr);
-- Print a location node that consists of
-- * an optional message node
-- * a physicalLocation node
-- * ArtifactLocation node that consists of the file name
-- * Region node that consists of the start and end positions of the span
--
-- {
-- "message": {
-- "text": <Msg>
-- },
-- "physicalLocation": {
-- "artifactLocation": {
-- "uri": <File_Name (Loc)>
-- },
-- "region": {
-- "startLine": <Line(Loc.Fst)>,
-- "startColumn": <Col(Loc.Fst)>,
-- "endLine": <Line(Loc.Lst)>,
-- "endColumn": Col(Loc.Lst)>
-- }
-- }
-- }
procedure Print_Locations (E_Msg : Error_Msg_Object);
-- Print a locations node that consists of multiple location nodes. However
-- typically just one location for the primary span of the diagnostic.
--
-- "locations": [
-- <Location (Primary_Span (Diag))>
-- ],
procedure Print_Message (Text : String; Name : String := N_MESSAGE);
-- Print a SARIF message node.
--
-- There are many message type nodes in the SARIF report however they can
-- have a different node <Name>.
--
-- <Name>: {
-- "text": <text>
-- },
procedure Print_Original_Uri_Base_Ids;
-- Print the originalUriBaseIds that holds the PWD value
--
-- "originalUriBaseIds": {
-- "PWD": {
-- "uri": "<current_working_directory>"
-- }
-- },
procedure Print_Related_Locations (E_Msg : Error_Msg_Object);
-- Print a relatedLocations node that consists of multiple location nodes.
-- Related locations are the non-primary spans of the diagnostic and the
-- primary locations of sub-diagnostics.
--
-- "relatedLocations": [
-- <Location (Diag.Loc)>
-- ],
procedure Print_Region
(Start_Line : Int;
Start_Col : Int;
End_Line : Int;
End_Col : Int;
Name : String := N_REGION);
-- Print a region node.
--
-- More specifically a text region node that specifies the textual
-- location of the region. Note that in SARIF there are also binary
-- regions.
--
-- "<Name>": {
-- "startLine": Start_Line,
-- "startColumn": Start_Col,
-- "endLine": End_Line,
-- "endColumn": End_Col + 1
-- }
--
-- Note that there are many types of nodes that can have a region type,
-- but have a different node name.
--
-- The end column is defined differently in the SARIF report than it is
-- for the spans within GNAT. Internally we consider the end column of a
-- span to be the last character of the span.
--
-- However in SARIF the end column is defined as:
-- "The column number of the character following the end of the region"
--
-- This method assumes that the End_Col passed to this procedure is using
-- the GNAT span definition and we amend the endColumn value so that it
-- matches the SARIF definition.
procedure Print_Result (E_Msg : Error_Msg_Object);
-- {
-- "ruleId": <Diag.Id>,
-- "level": <Diag.Kind>,
-- "message": {
-- "text": <Diag.Message>
-- },
-- "locations": [<Primary_Location>],
-- "relatedLocations": [<Secondary_Locations>]
-- },
procedure Print_Results;
-- Print a results node that consists of multiple result nodes for each
-- diagnostic instance.
--
-- "results": [
-- <Result (Diag)>
-- ]
procedure Print_Rule (E : Error_Msg_Object);
-- Print a rule node that consists of the following attributes:
-- * ruleId
-- * name
--
-- {
-- "id": <Diag.Id>,
-- "name": <Human_Id(Diag)>
-- },
procedure Print_Rules;
-- Print a rules node that consists of multiple rule nodes.
-- Rules are considered to be a set of unique diagnostics with the unique
-- id-s.
--
-- "rules": [
-- <Rule (Diag)>
-- ]
procedure Print_Runs;
-- Print a runs node that can consist of multiple run nodes.
-- However for our report it consists of a single run that consists of
-- * a tool node
-- * a results node
--
-- {
-- "tool": { <Tool (Diags)> },
-- "results": [<Results (Diags)>]
-- }
procedure Print_Tool;
-- Print a tool node that consists of
-- * a driver node that consists of:
-- * name
-- * version
-- * rules
--
-- "tool": {
-- "driver": {
-- "name": "GNAT",
-- "version": <GNAT_Version>,
-- "rules": [<Rules (Diags)>]
-- }
-- }
-------------
-- Destroy --
-------------
procedure Destroy (Elem : in out Artifact_Change) is
begin
Edit_Lists.Destroy (Elem.Replacements);
end Destroy;
--------------------------
-- Get_Artifact_Changes --
--------------------------
function Get_Artifact_Changes (Fix : Fix_Type) return Artifact_Change_List
is
procedure Insert (Changes : Artifact_Change_List; E : Edit_Type);
------------
-- Insert --
------------
procedure Insert (Changes : Artifact_Change_List; E : Edit_Type) is
A : Artifact_Change;
It : Artifact_Change_Lists.Iterator :=
Artifact_Change_Lists.Iterate (Changes);
begin
while Artifact_Change_Lists.Has_Next (It) loop
Artifact_Change_Lists.Next (It, A);
if A.File_Index = Get_Source_File_Index (E.Span.Ptr) then
Edit_Lists.Append (A.Replacements, E);
return;
end if;
end loop;
declare
Replacements : constant Edit_List := Edit_Lists.Create;
begin
Edit_Lists.Append (Replacements, E);
Artifact_Change_Lists.Append
(Changes,
(File_Index => Get_Source_File_Index (E.Span.Ptr),
Replacements => Replacements));
end;
end Insert;
Changes : constant Artifact_Change_List := Artifact_Change_Lists.Create;
E : Edit_Type;
It : Edit_Id;
-- Start of processing for Get_Artifact_Changes
begin
It := Fix.Edits;
while It /= No_Edit loop
E := Edits.Table (It);
Insert (Changes, E);
It := E.Next;
end loop;
return Changes;
end Get_Artifact_Changes;
----------------------
-- Get_Unique_Rules --
----------------------
function Get_Unique_Rules return Error_Msg_List is
use Error_Msg_Lists;
procedure Insert (Rules : Error_Msg_List; E : Error_Msg_Object);
------------
-- Insert --
------------
procedure Insert (Rules : Error_Msg_List; E : Error_Msg_Object) is
It : Iterator := Iterate (Rules);
R : Error_Msg_Object;
begin
while Has_Next (It) loop
Next (It, R);
if R.Id = E.Id then
return;
elsif R.Id > E.Id then
Insert_Before (Rules, R, E);
return;
end if;
end loop;
Append (Rules, E);
end Insert;
Unique_Rules : constant Error_Msg_List := Create;
E : Error_Msg_Id;
-- Start of processing for Get_Unique_Rules
begin
E := First_Error_Msg;
while E /= No_Error_Msg loop
Insert (Unique_Rules, Errors.Table (E));
Next_Error_Msg (E);
end loop;
return Unique_Rules;
end Get_Unique_Rules;
---------------------------
-- Print_Artifact_Change --
---------------------------
procedure Print_Artifact_Change (A : Artifact_Change) is
use Edit_Lists;
E : Edit_Type;
E_It : Iterator;
First : Boolean := True;
begin
Write_Char ('{');
Begin_Block;
NL_And_Indent;
-- Print artifactLocation
Print_Artifact_Location (A.File_Index);
Write_Char (',');
NL_And_Indent;
Write_Str ("""" & N_REPLACEMENTS & """" & ": " & "[");
Begin_Block;
NL_And_Indent;
E_It := Iterate (A.Replacements);
while Has_Next (E_It) loop
Next (E_It, E);
if First then
First := False;
else
Write_Char (',');
end if;
NL_And_Indent;
Print_Replacement (E);
end loop;
-- End replacements
End_Block;
NL_And_Indent;
Write_Char (']');
-- End artifactChange
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Artifact_Change;
-----------------------------
-- Print_Artifact_Location --
-----------------------------
procedure Print_Artifact_Location (Sfile : Source_File_Index) is
Full_Name : constant String := Get_Name_String (Full_Ref_Name (Sfile));
begin
Write_Str ("""" & N_ARTIFACT_LOCATION & """" & ": " & "{");
Begin_Block;
NL_And_Indent;
if System.OS_Lib.Is_Absolute_Path (Full_Name) then
declare
Abs_Name : constant String :=
System.OS_Lib.Normalize_Pathname
(Name => Full_Name, Resolve_Links => False);
begin
-- We cannot create relative paths between different drives on
-- Windows. If the path is on a different drive than the PWD print
-- the absolute path in the URI and omit the baseUriId attribute.
if Osint.On_Windows
and then Abs_Name (Abs_Name'First) =
Current_Dir (Current_Dir'First)
then
Write_String_Attribute (N_URI, To_File_Uri (Abs_Name));
else
Write_String_Attribute
(N_URI, To_File_Uri (Relative_Path (Abs_Name, Current_Dir)));
Write_Char (',');
NL_And_Indent;
Write_String_Attribute (N_URI_BASE_ID, URI_Base_Id_Name);
end if;
end;
else
-- If the path was not absolute it was given relative to the
-- uriBaseId.
Write_String_Attribute (N_URI, To_File_Uri (Full_Name));
Write_Char (',');
NL_And_Indent;
Write_String_Attribute (N_URI_BASE_ID, URI_Base_Id_Name);
end if;
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Artifact_Location;
-----------------------
-- Print_Replacement --
-----------------------
procedure Print_Replacement (Replacement : Edit_Type) is
-- Span start positions
Fst : constant Source_Ptr := Replacement.Span.First;
Line_Fst : constant Int := Int (Get_Physical_Line_Number (Fst));
Col_Fst : constant Int := Int (Get_Column_Number (Fst));
-- Span end positions
Lst : constant Source_Ptr := Replacement.Span.Last;
Line_Lst : constant Int := Int (Get_Physical_Line_Number (Lst));
Col_Lst : constant Int := Int (Get_Column_Number (Lst));
begin
Write_Char ('{');
Begin_Block;
NL_And_Indent;
-- Print deletedRegion
Print_Region
(Start_Line => Line_Fst,
Start_Col => Col_Fst,
End_Line => Line_Lst,
End_Col => Col_Lst,
Name => N_DELETED_REGION);
if Replacement.Text /= null then
Write_Char (',');
NL_And_Indent;
Print_Message (Replacement.Text.all, N_INSERTED_CONTENT);
end if;
-- End replacement
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Replacement;
---------------
-- Print_Fix --
---------------
procedure Print_Fix (Fix : Fix_Type) is
First : Boolean := True;
begin
Write_Char ('{');
Begin_Block;
NL_And_Indent;
-- Print the message if the location has one
if Fix.Description /= null then
Print_Message (Fix.Description.all, N_DESCRIPTION);
Write_Char (',');
NL_And_Indent;
end if;
declare
use Artifact_Change_Lists;
Changes : Artifact_Change_List := Get_Artifact_Changes (Fix);
A : Artifact_Change;
A_It : Iterator := Iterate (Changes);
begin
Write_Str ("""" & N_ARTIFACT_CHANGES & """" & ": " & "[");
Begin_Block;
while Has_Next (A_It) loop
Next (A_It, A);
if First then
First := False;
else
Write_Char (',');
end if;
NL_And_Indent;
Print_Artifact_Change (A);
end loop;
End_Block;
NL_And_Indent;
Write_Char (']');
Destroy (Changes);
end;
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Fix;
-----------------
-- Print_Fixes --
-----------------
procedure Print_Fixes (E_Msg : Error_Msg_Object) is
F : Fix_Type;
F_It : Fix_Id;
First : Boolean := True;
begin
Write_Str ("""" & N_FIXES & """" & ": " & "[");
Begin_Block;
F_It := E_Msg.Fixes;
while F_It /= No_Fix loop
F := Fixes.Table (F_It);
if First then
First := False;
else
Write_Char (',');
end if;
NL_And_Indent;
Print_Fix (F);
F_It := F.Next;
end loop;
End_Block;
NL_And_Indent;
Write_Char (']');
end Print_Fixes;
-----------------------
-- Print_Invocations --
-----------------------
procedure Print_Invocations is
function Compose_Command_Line return String;
-- Composes the original command line from the parsed main file name and
-- relevant compilation switches
function Compose_Command_Line return String is
Buffer : Bounded_String;
begin
Find_Program_Name;
Append (Buffer, Name_Buffer (1 .. Name_Len));
Append (Buffer, ' ');
Append (Buffer, Get_First_Main_File_Name);
for I in 1 .. Compilation_Switches_Last loop
declare
Switch : constant String := Get_Compilation_Switch (I).all;
begin
if Buffer.Length + Switch'Length + 1 <= Buffer.Max_Length then
Append (Buffer, ' ' & Switch);
end if;
end;
end loop;
return +Buffer;
end Compose_Command_Line;
begin
Write_Str ("""" & N_INVOCATIONS & """" & ": " & "[");
Begin_Block;
NL_And_Indent;
Write_Char ('{');
Begin_Block;
NL_And_Indent;
-- Print commandLine
Write_String_Attribute (N_COMMAND_LINE, Compose_Command_Line);
Write_Char (',');
NL_And_Indent;
-- Print executionSuccessful
Write_Boolean_Attribute (N_EXECUTION_SUCCESSFUL, Exit_Code = E_Success);
End_Block;
NL_And_Indent;
Write_Char ('}');
End_Block;
NL_And_Indent;
Write_Char (']');
end Print_Invocations;
------------------
-- Print_Region --
------------------
procedure Print_Region
(Start_Line : Int;
Start_Col : Int;
End_Line : Int;
End_Col : Int;
Name : String := N_REGION)
is
begin
Write_Str ("""" & Name & """" & ": " & "{");
Begin_Block;
NL_And_Indent;
Write_Int_Attribute (N_START_LINE, Start_Line);
Write_Char (',');
NL_And_Indent;
Write_Int_Attribute (N_START_COLUMN, Start_Col);
Write_Char (',');
NL_And_Indent;
Write_Int_Attribute (N_END_LINE, End_Line);
Write_Char (',');
NL_And_Indent;
-- Convert the end of the span to the definition of the endColumn
-- for a SARIF region.
Write_Int_Attribute (N_END_COLUMN, End_Col + 1);
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Region;
--------------------
-- Print_Location --
--------------------
procedure Print_Location (Loc : Labeled_Span_Type; Msg : String_Ptr) is
-- Span start positions
Fst : constant Source_Ptr := Loc.Span.First;
Line_Fst : constant Int := Int (Get_Physical_Line_Number (Fst));
Col_Fst : constant Int := Int (Get_Column_Number (Fst));
-- Span end positions
Lst : constant Source_Ptr := Loc.Span.Last;
Line_Lst : constant Int := Int (Get_Physical_Line_Number (Lst));
Col_Lst : constant Int := Int (Get_Column_Number (Lst));
begin
Write_Char ('{');
Begin_Block;
NL_And_Indent;
-- Print the message if the location has one
if Msg /= null then
Print_Message (Msg.all);
Write_Char (',');
NL_And_Indent;
end if;
Write_Str ("""" & N_PHYSICAL_LOCATION & """" & ": " & "{");
Begin_Block;
NL_And_Indent;
-- Print artifactLocation
Print_Artifact_Location (Get_Source_File_Index (Loc.Span.Ptr));
Write_Char (',');
NL_And_Indent;
-- Print region
Print_Region
(Start_Line => Line_Fst,
Start_Col => Col_Fst,
End_Line => Line_Lst,
End_Col => Col_Lst);
End_Block;
NL_And_Indent;
Write_Char ('}');
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Location;
---------------------
-- Print_Locations --
---------------------
procedure Print_Locations (E_Msg : Error_Msg_Object) is
Loc : Labeled_Span_Type;
It : Labeled_Span_Id;
First : Boolean := True;
begin
Write_Str ("""" & N_LOCATIONS & """" & ": " & "[");
Begin_Block;
It := E_Msg.Locations;
while It /= No_Labeled_Span loop
Loc := Locations.Table (It);
-- Only the primary span is considered as the main location other
-- spans are considered related locations
if Loc.Is_Primary then
if First then
First := False;
else
Write_Char (',');
end if;
NL_And_Indent;
Print_Location (Loc, Loc.Label);
end if;
It := Loc.Next;
end loop;
End_Block;
NL_And_Indent;
Write_Char (']');
end Print_Locations;
-------------------
-- Print_Message --
-------------------
procedure Print_Message (Text : String; Name : String := N_MESSAGE) is
begin
Write_Str ("""" & Name & """" & ": " & "{");
Begin_Block;
NL_And_Indent;
Write_String_Attribute (N_TEXT, Text);
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Message;
---------------------------------
-- Print_Original_Uri_Base_Ids --
---------------------------------
procedure Print_Original_Uri_Base_Ids is
begin
Write_Str ("""" & N_ORIGINAL_URI_BASE_IDS & """" & ": " & "{");
Begin_Block;
NL_And_Indent;
Write_Str ("""" & URI_Base_Id_Name & """" & ": " & "{");
Begin_Block;
NL_And_Indent;
Write_String_Attribute (N_URI, To_File_Uri (Current_Dir));
End_Block;
NL_And_Indent;
Write_Char ('}');
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Original_Uri_Base_Ids;
-----------------------------
-- Print_Related_Locations --
-----------------------------
procedure Print_Related_Locations (E_Msg : Error_Msg_Object) is
Loc : Labeled_Span_Type;
Loc_It : Labeled_Span_Id;
Sub : Error_Msg_Object;
Sub_It : Error_Msg_Id;
First : Boolean := True;
begin
Write_Str ("""" & N_RELATED_LOCATIONS & """" & ": " & "[");
Begin_Block;
-- Related locations are the non-primary spans of the diagnostic
Loc_It := E_Msg.Locations;
while Loc_It /= No_Labeled_Span loop
Loc := Locations.Table (Loc_It);
-- Non-primary spans are considered related locations
if not Loc.Is_Primary then
if First then
First := False;
else
Write_Char (',');
end if;
NL_And_Indent;
Print_Location (Loc, Loc.Label);
end if;
Loc_It := Loc.Next;
end loop;
-- And the sub-diagnostic locations
Sub_It := E_Msg.Next;
while Sub_It /= No_Error_Msg and then Errors.Table (Sub_It).Msg_Cont loop
Sub := Errors.Table (Sub_It);
declare
Found : Boolean := False;
Prim_Loc_Id : Labeled_Span_Id;
begin
Prim_Loc_Id := Primary_Location (Sub);
if Prim_Loc_Id /= No_Labeled_Span then
Found := True;
else
Prim_Loc_Id := Primary_Location (E_Msg);
Found := True;
end if;
-- For mapping sub-diagnostics to related locations we have to
-- make some compromises in details.
--
-- Firstly we only make one entry that is for the primary span
-- of the sub-diagnostic.
--
-- Secondly this span can also have a label. However this
-- pattern is not advised and by default we include the message
-- of the sub-diagnostic as the message in location node since
-- it should have more information.
if Found then
if First then
First := False;
else
Write_Char (',');
end if;
NL_And_Indent;
Print_Location (Locations.Table (Prim_Loc_Id), Sub.Text);
end if;
end;
Next_Continuation_Msg (Sub_It);
end loop;
End_Block;
NL_And_Indent;
Write_Char (']');
end Print_Related_Locations;
------------------
-- Print_Result --
------------------
procedure Print_Result (E_Msg : Error_Msg_Object) is
begin
Write_Char ('{');
Begin_Block;
NL_And_Indent;
-- Print ruleId
Write_String_Attribute (N_RULE_ID, "[" & To_String (E_Msg.Id) & "]");
Write_Char (',');
NL_And_Indent;
-- Print level
Write_String_Attribute (N_LEVEL, Kind_To_String (E_Msg));
Write_Char (',');
NL_And_Indent;
-- Print message
Print_Message (E_Msg.Text.all);
Write_Char (',');
NL_And_Indent;
-- Print locations
Print_Locations (E_Msg);
Write_Char (',');
NL_And_Indent;
-- Print related locations
Print_Related_Locations (E_Msg);
Write_Char (',');
NL_And_Indent;
-- Print fixes
Print_Fixes (E_Msg);
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Result;
-------------------
-- Print_Results --
-------------------
procedure Print_Results is
E : Error_Msg_Id;
First : Boolean := True;
begin
Write_Str ("""" & N_RESULTS & """" & ": " & "[");
Begin_Block;
E := First_Error_Msg;
while E /= No_Error_Msg loop
if First then
First := False;
else
Write_Char (',');
end if;
NL_And_Indent;
Print_Result (Errors.Table (E));
Next_Error_Msg (E);
end loop;
End_Block;
NL_And_Indent;
Write_Char (']');
end Print_Results;
----------------
-- Print_Rule --
----------------
procedure Print_Rule (E : Error_Msg_Object) is
Human_Id : constant String_Ptr := Get_Human_Id (E);
begin
Write_Char ('{');
Begin_Block;
NL_And_Indent;
Write_String_Attribute (N_ID, "[" & To_String (E.Id) & "]");
Write_Char (',');
NL_And_Indent;
if Human_Id = null then
Write_String_Attribute (N_NAME, "Uncategorized_Diagnostic");
else
Write_String_Attribute (N_NAME, Human_Id.all);
end if;
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Rule;
-----------------
-- Print_Rules --
-----------------
procedure Print_Rules is
use Error_Msg_Lists;
R : Error_Msg_Object;
Rules : Error_Msg_List := Get_Unique_Rules;
It : Iterator := Iterate (Rules);
First : Boolean := True;
begin
Write_Str ("""" & N_RULES & """" & ": " & "[");
Begin_Block;
while Has_Next (It) loop
Next (It, R);
if First then
First := False;
else
Write_Char (',');
end if;
NL_And_Indent;
Print_Rule (R);
end loop;
End_Block;
NL_And_Indent;
Write_Char (']');
Error_Msg_Lists.Destroy (Rules);
end Print_Rules;
----------------
-- Print_Tool --
----------------
procedure Print_Tool is
begin
Write_Str ("""" & N_TOOL & """" & ": " & "{");
Begin_Block;
NL_And_Indent;
-- -- Attributes of tool
Write_Str ("""" & N_DRIVER & """" & ": " & "{");
Begin_Block;
NL_And_Indent;
-- Attributes of tool.driver
Write_String_Attribute (N_NAME, "GNAT");
Write_Char (',');
NL_And_Indent;
Write_String_Attribute (N_VERSION, Gnat_Version_String);
Write_Char (',');
NL_And_Indent;
Print_Rules;
-- End of tool.driver
End_Block;
NL_And_Indent;
Write_Char ('}');
-- End of tool
End_Block;
NL_And_Indent;
Write_Char ('}');
end Print_Tool;
----------------
-- Print_Runs --
----------------
procedure Print_Runs is
begin
Write_Str ("""" & N_RUNS & """" & ": " & "[");
Begin_Block;
NL_And_Indent;
-- Runs can consist of multiple "run"-s. However the GNAT SARIF report
-- only has one.
Write_Char ('{');
Begin_Block;
NL_And_Indent;
-- A run consists of a tool
Print_Tool;
Write_Char (',');
NL_And_Indent;
-- A run consists of an invocation
Print_Invocations;
Write_Char (',');
NL_And_Indent;
Print_Original_Uri_Base_Ids;
Write_Char (',');
NL_And_Indent;
-- A run consists of results
Print_Results;
-- End of run
End_Block;
NL_And_Indent;
Write_Char ('}');
End_Block;
NL_And_Indent;
-- End of runs
Write_Char (']');
end Print_Runs;
------------------------
-- Print_SARIF_Report --
------------------------
procedure Print_SARIF_Report is
begin
Write_Char ('{');
Begin_Block;
NL_And_Indent;
Write_String_Attribute (N_SCHEMA, SARIF_Schema);
Write_Char (',');
NL_And_Indent;
Write_String_Attribute (N_VERSION, SARIF_Version);
Write_Char (',');
NL_And_Indent;
Print_Runs;
End_Block;
NL_And_Indent;
Write_Char ('}');
Write_Eol;
end Print_SARIF_Report;
end Erroutc.SARIF_Emitter;
|