Debian Codegraph @

gcc/ada/diagnostics-sarif_emitter.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- D I A G N O S T I C S . S A R I F _ 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 Diagnostics.JSON_Utils; use Diagnostics.JSON_Utils;
28 with Gnatvsn; use Gnatvsn;
29 with Output; use Output;
30 with Sinput; use Sinput;
31 with Lib; use Lib;
32 with Namet; use Namet;
33 with Osint; use Osint;
34 with Errout; use Errout;
35
36 package body Diagnostics.SARIF_Emitter is
37
38 -- We are currently using SARIF 2.1.0
39
40 SARIF_Version : constant String := "2.1.0";
41 pragma Style_Checks ("M100");
42 SARIF_Schema : constant String :=
43 "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json";
44 pragma Style_Checks ("M79");
45
46 type Artifact_Change is record
47 File : String_Ptr;
48 -- Name of the file
49
50 Replacements : Edit_List;
51 -- Regions of texts to be edited
52 end record;
53
54 procedure Destroy (Elem : in out Artifact_Change);
55 pragma Inline (Destroy);
56
57 function Equals (L, R : Artifact_Change) return Boolean is
58 (L.File /= null
59 and then R.File /= null
60 and then L.File.all = R.File.all);
61
62 package Artifact_Change_Lists is new Doubly_Linked_Lists
63 (Element_Type => Artifact_Change,
64 "=" => Equals,
65 Destroy_Element => Destroy,
66 Check_Tampering => False);
67
68 subtype Artifact_Change_List is Artifact_Change_Lists.Doubly_Linked_List;
69
70 function Get_Artifact_Changes (Fix : Fix_Type) return Artifact_Change_List;
71 -- Group edits of a Fix into Artifact_Changes that organize the edits by
72 -- file name.
73
74 function Get_Unique_Rules (Diags : Diagnostic_List) return Diagnostic_List;
75 -- Get a list of diagnostics that have unique Diagnostic Id-s.
76
77 procedure Print_Replacement (Replacement : Edit_Type);
78 -- Print a replacement node
79 --
80 -- {
81 -- deletedRegion: {<Region>},
82 -- insertedContent: {<Message>}
83 -- }
84
85 procedure Print_Fix (Fix : Fix_Type);
86 -- Print the fix node
87 --
88 -- {
89 -- description: {<Message>},
90 -- artifactChanges: [<ArtifactChange>]
91 -- }
92
93 procedure Print_Fixes (Diag : Diagnostic_Type);
94 -- Print the fixes node
95 --
96 -- "fixes": [
97 -- <Fix>,
98 -- ...
99 -- ]
100
101 procedure Print_Invocations;
102 -- Print an invocations node that consists of
103 -- * a single invocation node that consists of:
104 -- * commandLine
105 -- * executionSuccessful
106 --
107 -- "invocations": [
108 -- {
109 -- "commandLine": <command line arguments provided to the GNAT FE>,
110 -- "executionSuccessful": ["true"|"false"],
111 -- }
112 -- ]
113
114 procedure Print_Artifact_Change (A : Artifact_Change);
115 -- Print an ArtifactChange node
116 --
117 -- {
118 -- artifactLocation: {<ArtifactLocation>},
119 -- replacements: [<Replacements>]
120 -- }
121
122 procedure Print_Artifact_Location (File_Name : String);
123 -- Print an artifactLocation node
124 --
125 -- "artifactLocation": {
126 -- "URI": <File_Name>
127 -- }
128
129 procedure Print_Location (Loc : Labeled_Span_Type;
130 Msg : String_Ptr);
131 -- Print a location node that consists of
132 -- * an optional message node
133 -- * a physicalLocation node
134 -- * ArtifactLocation node that consists of the file name
135 -- * Region node that consists of the start and end positions of the span
136 --
137 -- {
138 -- "message": {
139 -- "text": <Msg>
140 -- },
141 -- "physicalLocation": {
142 -- "artifactLocation": {
143 -- "URI": <File_Name (Loc)>
144 -- },
145 -- "region": {
146 -- "startLine": <Line(Loc.Fst)>,
147 -- "startColumn": <Col(Loc.Fst)>,
148 -- "endLine": <Line(Loc.Lst)>,
149 -- "endColumn": Col(Loc.Lst)>
150 -- }
151 -- }
152 -- }
153
154 procedure Print_Locations (Diag : Diagnostic_Type);
155 -- Print a locations node that consists of multiple location nodes. However
156 -- typically just one location for the primary span of the diagnostic.
157 --
158 -- "locations": [
159 -- <Location (Primary_Span (Diag))>
160 -- ],
161
162 procedure Print_Message (Text : String; Name : String := "message");
163 -- Print a SARIF message node
164 --
165 -- "message": {
166 -- "text": <text>
167 -- },
168
169 procedure Print_Related_Locations (Diag : Diagnostic_Type);
170 -- Print a relatedLocations node that consists of multiple location nodes.
171 -- Related locations are the non-primary spans of the diagnostic and the
172 -- primary locations of sub-diagnostics.
173 --
174 -- "relatedLocations": [
175 -- <Location (Diag.Loc)>
176 -- ],
177
178 procedure Print_Region (Start_Line : Int;
179 Start_Col : Int;
180 End_Line : Int;
181 End_Col : Int;
182 Name : String := "region");
183 -- Print a region node.
184 --
185 -- More specifically a text region node that specifies the textual
186 -- location of the region. Note that in SARIF there are also binary
187 -- regions.
188 --
189 -- "<Name>": {
190 -- "startLine": Start_Line,
191 -- "startColumn": Start_Col,
192 -- "endLine": End_Line,
193 -- "endColumn": End_Col + 1
194 -- }
195 --
196 -- Note that there are many types of nodes that can have a region type,
197 -- but have a different node name.
198 --
199 -- The end column is defined differently in the SARIF report than it is
200 -- for the spans within GNAT. Internally we consider the end column of a
201 -- span to be the last character of the span.
202 --
203 -- However in SARIF the end column is defined as:
204 -- "The column number of the character following the end of the region"
205 --
206 -- This method assumes that the End_Col passed to this procedure is using
207 -- the GNAT span definition and we amend the endColumn value so that it
208 -- matches the SARIF definition.
209
210 procedure Print_Result (Diag : Diagnostic_Type);
211 -- {
212 -- "ruleId": <Diag.Id>,
213 -- "level": <Diag.Kind>,
214 -- "message": {
215 -- "text": <Diag.Message>
216 -- },
217 -- "locations": [<Primary_Location>],
218 -- "relatedLocations": [<Secondary_Locations>]
219 -- },
220
221 procedure Print_Results (Diags : Diagnostic_List);
222 -- Print a results node that consists of multiple result nodes for each
223 -- diagnostic instance.
224 --
225 -- "results": [
226 -- <Result (Diag)>
227 -- ]
228
229 procedure Print_Rule (Diag : Diagnostic_Type);
230 -- Print a rule node that consists of the following attributes:
231 -- * ruleId
232 -- * name
233 --
234 -- {
235 -- "id": <Diag.Id>,
236 -- "name": <Human_Id(Diag)>
237 -- },
238
239 procedure Print_Rules (Diags : Diagnostic_List);
240 -- Print a rules node that consists of multiple rule nodes.
241 -- Rules are considered to be a set of unique diagnostics with the unique
242 -- id-s.
243 --
244 -- "rules": [
245 -- <Rule (Diag)>
246 -- ]
247
248 procedure Print_Runs (Diags : Diagnostic_List);
249 -- Print a runs node that can consist of multiple run nodes.
250 -- However for our report it consists of a single run that consists of
251 -- * a tool node
252 -- * a results node
253 --
254 -- {
255 -- "tool": { <Tool (Diags)> },
256 -- "results": [<Results (Diags)>]
257 -- }
258
259 procedure Print_Tool (Diags : Diagnostic_List);
260 -- Print a tool node that consists of
261 -- * a driver node that consists of:
262 -- * name
263 -- * version
264 -- * rules
265 --
266 -- "tool": {
267 -- "driver": {
268 -- "name": "GNAT",
269 -- "version": <GNAT_Version>,
270 -- "rules": [<Rules (Diags)>]
271 -- }
272 -- }
273
274 -------------
275 -- Destroy --
276 -------------
277
278 procedure Destroy (Elem : in out Artifact_Change)
279 is
280
281 begin
282 Free (Elem.File);
283 end Destroy;
284
285 --------------------------
286 -- Get_Artifact_Changes --
287 --------------------------
288
289 function Get_Artifact_Changes (Fix : Fix_Type) return Artifact_Change_List
290 is
291 procedure Insert (Changes : Artifact_Change_List; E : Edit_Type);
292
293 ------------
294 -- Insert --
295 ------------
296
297 procedure Insert (Changes : Artifact_Change_List; E : Edit_Type)
298 is
299 A : Artifact_Change;
300
301 It : Artifact_Change_Lists.Iterator :=
302 Artifact_Change_Lists.Iterate (Changes);
303 begin
304 while Artifact_Change_Lists.Has_Next (It) loop
305 Artifact_Change_Lists.Next (It, A);
306
307 if A.File.all = To_File_Name (E.Span.Ptr) then
308 Edit_Lists.Append (A.Replacements, E);
309 return;
310 end if;
311 end loop;
312
313 declare
314 Replacements : constant Edit_List := Edit_Lists.Create;
315 begin
316 Edit_Lists.Append (Replacements, E);
317 Artifact_Change_Lists.Append
318 (Changes,
319 (File => new String'(To_File_Name (E.Span.Ptr)),
320 Replacements => Replacements));
321 end;
322 end Insert;
323
324 Changes : constant Artifact_Change_List := Artifact_Change_Lists.Create;
325
326 E : Edit_Type;
327
328 It : Edit_Lists.Iterator := Edit_Lists.Iterate (Fix.Edits);
329 begin
330 while Edit_Lists.Has_Next (It) loop
331 Edit_Lists.Next (It, E);
332
333 Insert (Changes, E);
334 end loop;
335
336 return Changes;
337 end Get_Artifact_Changes;
338
339 ----------------------
340 -- Get_Unique_Rules --
341 ----------------------
342
343 function Get_Unique_Rules (Diags : Diagnostic_List)
344 return Diagnostic_List
345 is
346 use Diagnostics.Diagnostics_Lists;
347
348 procedure Insert (Rules : Diagnostic_List; D : Diagnostic_Type);
349
350 ------------
351 -- Insert --
352 ------------
353
354 procedure Insert (Rules : Diagnostic_List; D : Diagnostic_Type) is
355 It : Iterator := Iterate (Rules);
356 R : Diagnostic_Type;
357 begin
358 while Has_Next (It) loop
359 Next (It, R);
360
361 if R.Id = D.Id then
362 return;
363 elsif R.Id > D.Id then
364 Insert_Before (Rules, R, D);
365 return;
366 end if;
367 end loop;
368
369 Append (Rules, D);
370 end Insert;
371
372 D : Diagnostic_Type;
373 Unique_Rules : constant Diagnostic_List := Create;
374
375 It : Iterator := Iterate (Diags);
376 begin
377 if Present (Diags) then
378 while Has_Next (It) loop
379 Next (It, D);
380 Insert (Unique_Rules, D);
381 end loop;
382 end if;
383
384 return Unique_Rules;
385 end Get_Unique_Rules;
386
387 ---------------------------
388 -- Print_Artifact_Change --
389 ---------------------------
390
391 procedure Print_Artifact_Change (A : Artifact_Change)
392 is
393 use Diagnostics.Edit_Lists;
394 E : Edit_Type;
395 E_It : Iterator;
396
397 First : Boolean := True;
398 begin
399 Write_Char ('{');
400 Begin_Block;
401 NL_And_Indent;
402
403 -- Print artifactLocation
404
405 Print_Artifact_Location (A.File.all);
406
407 Write_Char (',');
408 NL_And_Indent;
409
410 Write_Str ("""" & "replacements" & """" & ": " & "[");
411 Begin_Block;
412 NL_And_Indent;
413
414 E_It := Iterate (A.Replacements);
415
416 while Has_Next (E_It) loop
417 Next (E_It, E);
418
419 if First then
420 First := False;
421 else
422 Write_Char (',');
423 end if;
424
425 NL_And_Indent;
426 Print_Replacement (E);
427 end loop;
428
429 -- End replacements
430
431 End_Block;
432 NL_And_Indent;
433 Write_Char (']');
434
435 -- End artifactChange
436
437 End_Block;
438 NL_And_Indent;
439 Write_Char ('}');
440 end Print_Artifact_Change;
441
442 -----------------------------
443 -- Print_Artifact_Location --
444 -----------------------------
445
446 procedure Print_Artifact_Location (File_Name : String) is
447
448 begin
449 Write_Str ("""" & "artifactLocation" & """" & ": " & "{");
450 Begin_Block;
451 NL_And_Indent;
452
453 Write_String_Attribute ("uri", File_Name);
454
455 End_Block;
456 NL_And_Indent;
457 Write_Char ('}');
458 end Print_Artifact_Location;
459
460 -----------------------
461 -- Print_Replacement --
462 -----------------------
463
464 procedure Print_Replacement (Replacement : Edit_Type) is
465 -- Span start positions
466 Fst : constant Source_Ptr := Replacement.Span.First;
467 Line_Fst : constant Int := Int (Get_Physical_Line_Number (Fst));
468 Col_Fst : constant Int := Int (Get_Column_Number (Fst));
469
470 -- Span end positions
471 Lst : constant Source_Ptr := Replacement.Span.Last;
472 Line_Lst : constant Int := Int (Get_Physical_Line_Number (Lst));
473 Col_Lst : constant Int := Int (Get_Column_Number (Lst));
474 begin
475 Write_Char ('{');
476 Begin_Block;
477 NL_And_Indent;
478
479 -- Print deletedRegion
480
481 Print_Region (Start_Line => Line_Fst,
482 Start_Col => Col_Fst,
483 End_Line => Line_Lst,
484 End_Col => Col_Lst,
485 Name => "deletedRegion");
486
487 if Replacement.Text /= null then
488 Write_Char (',');
489 NL_And_Indent;
490
491 Print_Message (Replacement.Text.all, "insertedContent");
492 end if;
493
494 -- End replacement
495
496 End_Block;
497 NL_And_Indent;
498 Write_Char ('}');
499 end Print_Replacement;
500
501 ---------------
502 -- Print_Fix --
503 ---------------
504
505 procedure Print_Fix (Fix : Fix_Type) is
506 First : Boolean := True;
507 begin
508 Write_Char ('{');
509 Begin_Block;
510 NL_And_Indent;
511
512 -- Print the message if the location has one
513
514 if Fix.Description /= null then
515 Print_Message (Fix.Description.all, "description");
516
517 Write_Char (',');
518 NL_And_Indent;
519 end if;
520
521 declare
522 use Artifact_Change_Lists;
523 Changes : Artifact_Change_List := Get_Artifact_Changes (Fix);
524 A : Artifact_Change;
525 A_It : Iterator := Iterate (Changes);
526 begin
527 Write_Str ("""" & "artifactChanges" & """" & ": " & "[");
528 Begin_Block;
529
530 while Has_Next (A_It) loop
531 Next (A_It, A);
532
533 if First then
534 First := False;
535 else
536 Write_Char (',');
537 end if;
538
539 NL_And_Indent;
540
541 Print_Artifact_Change (A);
542 end loop;
543
544 End_Block;
545 NL_And_Indent;
546 Write_Char (']');
547
548 Destroy (Changes);
549 end;
550
551 End_Block;
552 NL_And_Indent;
553 Write_Char ('}');
554 end Print_Fix;
555
556 -----------------
557 -- Print_Fixes --
558 -----------------
559
560 procedure Print_Fixes (Diag : Diagnostic_Type) is
561 use Diagnostics.Fix_Lists;
562 F : Fix_Type;
563 F_It : Iterator;
564
565 First : Boolean := True;
566 begin
567 Write_Str ("""" & "fixes" & """" & ": " & "[");
568 Begin_Block;
569
570 if Present (Diag.Fixes) then
571 F_It := Iterate (Diag.Fixes);
572 while Has_Next (F_It) loop
573 Next (F_It, F);
574
575 if First then
576 First := False;
577 else
578 Write_Char (',');
579 end if;
580
581 NL_And_Indent;
582 Print_Fix (F);
583 end loop;
584 end if;
585
586 End_Block;
587 NL_And_Indent;
588 Write_Char (']');
589 end Print_Fixes;
590
591 -----------------------
592 -- Print_Invocations --
593 -----------------------
594
595 procedure Print_Invocations is
596
597 function Compose_Command_Line return String;
598 -- Composes the original command line from the parsed main file name and
599 -- relevant compilation switches
600
601 function Compose_Command_Line return String is
602 Buffer : Bounded_String;
603 begin
604 Append (Buffer, Get_First_Main_File_Name);
605 for I in 1 .. Compilation_Switches_Last loop
606 declare
607 Switch : constant String := Get_Compilation_Switch (I).all;
608 begin
609 if Buffer.Length + Switch'Length + 1 <= Buffer.Max_Length then
610 Append (Buffer, ' ' & Switch);
611 end if;
612 end;
613 end loop;
614
615 return +Buffer;
616 end Compose_Command_Line;
617
618 begin
619 Write_Str ("""" & "invocations" & """" & ": " & "[");
620 Begin_Block;
621 NL_And_Indent;
622
623 Write_Char ('{');
624 Begin_Block;
625 NL_And_Indent;
626
627 -- Print commandLine
628
629 Write_String_Attribute ("commandLine", Compose_Command_Line);
630 Write_Char (',');
631 NL_And_Indent;
632
633 -- Print executionSuccessful
634
635 Write_Boolean_Attribute ("executionSuccessful", Compilation_Errors);
636
637 End_Block;
638 NL_And_Indent;
639 Write_Char ('}');
640
641 End_Block;
642 NL_And_Indent;
643 Write_Char (']');
644 end Print_Invocations;
645
646 ------------------
647 -- Print_Region --
648 ------------------
649
650 procedure Print_Region (Start_Line : Int;
651 Start_Col : Int;
652 End_Line : Int;
653 End_Col : Int;
654 Name : String := "region")
655 is
656
657 begin
658 Write_Str ("""" & Name & """" & ": " & "{");
659 Begin_Block;
660 NL_And_Indent;
661
662 Write_Int_Attribute ("startLine", Start_Line);
663 Write_Char (',');
664 NL_And_Indent;
665
666 Write_Int_Attribute ("startColumn", Start_Col);
667 Write_Char (',');
668 NL_And_Indent;
669
670 Write_Int_Attribute ("endLine", End_Line);
671 Write_Char (',');
672 NL_And_Indent;
673
674 -- Convert the end of the span to the definition of the endColumn
675 -- for a SARIF region.
676
677 Write_Int_Attribute ("endColumn", End_Col + 1);
678
679 End_Block;
680 NL_And_Indent;
681 Write_Char ('}');
682 end Print_Region;
683
684 --------------------
685 -- Print_Location --
686 --------------------
687
688 procedure Print_Location (Loc : Labeled_Span_Type;
689 Msg : String_Ptr)
690 is
691
692 -- Span start positions
693 Fst : constant Source_Ptr := Loc.Span.First;
694 Line_Fst : constant Int := Int (Get_Physical_Line_Number (Fst));
695 Col_Fst : constant Int := Int (Get_Column_Number (Fst));
696
697 -- Span end positions
698 Lst : constant Source_Ptr := Loc.Span.Last;
699 Line_Lst : constant Int := Int (Get_Physical_Line_Number (Lst));
700 Col_Lst : constant Int := Int (Get_Column_Number (Lst));
701
702 begin
703 Write_Char ('{');
704 Begin_Block;
705 NL_And_Indent;
706
707 -- Print the message if the location has one
708
709 if Msg /= null then
710 Print_Message (Msg.all);
711
712 Write_Char (',');
713 NL_And_Indent;
714 end if;
715
716 Write_Str ("""" & "physicalLocation" & """" & ": " & "{");
717 Begin_Block;
718 NL_And_Indent;
719
720 -- Print artifactLocation
721
722 Print_Artifact_Location (To_File_Name (Loc.Span.Ptr));
723
724 Write_Char (',');
725 NL_And_Indent;
726
727 -- Print region
728
729 Print_Region (Start_Line => Line_Fst,
730 Start_Col => Col_Fst,
731 End_Line => Line_Lst,
732 End_Col => Col_Lst);
733
734 End_Block;
735 NL_And_Indent;
736 Write_Char ('}');
737
738 End_Block;
739 NL_And_Indent;
740 Write_Char ('}');
741 end Print_Location;
742
743 ---------------------
744 -- Print_Locations --
745 ---------------------
746
747 procedure Print_Locations (Diag : Diagnostic_Type) is
748 use Diagnostics.Labeled_Span_Lists;
749 Loc : Labeled_Span_Type;
750 It : Iterator := Iterate (Diag.Locations);
751
752 First : Boolean := True;
753 begin
754 Write_Str ("""" & "locations" & """" & ": " & "[");
755 Begin_Block;
756
757 while Has_Next (It) loop
758 Next (It, Loc);
759
760 -- Only the primary span is considered as the main location other
761 -- spans are considered related locations
762
763 if Loc.Is_Primary then
764 if First then
765 First := False;
766 else
767 Write_Char (',');
768 end if;
769
770 NL_And_Indent;
771 Print_Location (Loc, Loc.Label);
772 end if;
773 end loop;
774
775 End_Block;
776 NL_And_Indent;
777 Write_Char (']');
778
779 end Print_Locations;
780
781 -------------------
782 -- Print_Message --
783 -------------------
784
785 procedure Print_Message (Text : String; Name : String := "message") is
786
787 begin
788 Write_Str ("""" & Name & """" & ": " & "{");
789 Begin_Block;
790 NL_And_Indent;
791 Write_String_Attribute ("text", Text);
792 End_Block;
793 NL_And_Indent;
794 Write_Char ('}');
795 end Print_Message;
796
797 -----------------------------
798 -- Print_Related_Locations --
799 -----------------------------
800
801 procedure Print_Related_Locations (Diag : Diagnostic_Type) is
802 Loc : Labeled_Span_Type;
803 Loc_It : Labeled_Span_Lists.Iterator :=
804 Labeled_Span_Lists.Iterate (Diag.Locations);
805
806 Sub : Sub_Diagnostic_Type;
807 Sub_It : Sub_Diagnostic_Lists.Iterator;
808
809 First : Boolean := True;
810 begin
811 Write_Str ("""" & "relatedLocations" & """" & ": " & "[");
812 Begin_Block;
813
814 -- Related locations are the non-primary spans of the diagnostic
815
816 while Labeled_Span_Lists.Has_Next (Loc_It) loop
817 Labeled_Span_Lists.Next (Loc_It, Loc);
818
819 -- Non-primary spans are considered related locations
820
821 if not Loc.Is_Primary then
822 if First then
823 First := False;
824 else
825 Write_Char (',');
826 end if;
827
828 NL_And_Indent;
829 Print_Location (Loc, Loc.Label);
830 end if;
831 end loop;
832
833 -- And the sub-diagnostic locations
834
835 if Sub_Diagnostic_Lists.Present (Diag.Sub_Diagnostics) then
836 Sub_It := Sub_Diagnostic_Lists.Iterate (Diag.Sub_Diagnostics);
837
838 while Sub_Diagnostic_Lists.Has_Next (Sub_It) loop
839 Sub_Diagnostic_Lists.Next (Sub_It, Sub);
840
841 declare
842 Found : Boolean := False;
843
844 Prim_Loc : Labeled_Span_Type;
845 begin
846 if Labeled_Span_Lists.Present (Sub.Locations) then
847 Loc_It := Labeled_Span_Lists.Iterate (Sub.Locations);
848 while Labeled_Span_Lists.Has_Next (Loc_It) loop
849 Labeled_Span_Lists.Next (Loc_It, Loc);
850
851 -- For sub-diagnostic locations, only the primary span is
852 -- considered.
853
854 if not Found and then Loc.Is_Primary then
855 Found := True;
856 Prim_Loc := Loc;
857 end if;
858 end loop;
859 else
860
861 -- If there are no locations for the sub-diagnostic then use
862 -- the primary location of the main diagnostic.
863
864 Found := True;
865 Prim_Loc := Primary_Location (Diag);
866 end if;
867
868 -- For mapping sub-diagnostics to related locations we have to
869 -- make some compromises in details.
870 --
871 -- Firstly we only make one entry that is for the primary span
872 -- of the sub-diagnostic.
873 --
874 -- Secondly this span can also have a label. However this
875 -- pattern is not advised and by default we include the message
876 -- of the sub-diagnostic as the message in location node since
877 -- it should have more information.
878
879 if Found then
880 if First then
881 First := False;
882 else
883 Write_Char (',');
884 end if;
885 NL_And_Indent;
886 Print_Location (Prim_Loc, Sub.Message);
887 end if;
888 end;
889 end loop;
890 end if;
891
892 End_Block;
893 NL_And_Indent;
894 Write_Char (']');
895
896 end Print_Related_Locations;
897
898 ------------------
899 -- Print_Result --
900 ------------------
901
902 procedure Print_Result (Diag : Diagnostic_Type) is
903
904 begin
905 Write_Char ('{');
906 Begin_Block;
907 NL_And_Indent;
908
909 -- Print ruleId
910
911 Write_String_Attribute ("ruleId", "[" & To_String (Diag.Id) & "]");
912
913 Write_Char (',');
914 NL_And_Indent;
915
916 -- Print level
917
918 Write_String_Attribute ("level", Kind_To_String (Diag));
919
920 Write_Char (',');
921 NL_And_Indent;
922
923 -- Print message
924
925 Print_Message (Diag.Message.all);
926
927 Write_Char (',');
928 NL_And_Indent;
929
930 -- Print locations
931
932 Print_Locations (Diag);
933
934 Write_Char (',');
935 NL_And_Indent;
936
937 -- Print related locations
938
939 Print_Related_Locations (Diag);
940
941 Write_Char (',');
942 NL_And_Indent;
943
944 -- Print fixes
945
946 Print_Fixes (Diag);
947
948 End_Block;
949 NL_And_Indent;
950
951 Write_Char ('}');
952 end Print_Result;
953
954 -------------------
955 -- Print_Results --
956 -------------------
957
958 procedure Print_Results (Diags : Diagnostic_List) is
959 use Diagnostics.Diagnostics_Lists;
960
961 D : Diagnostic_Type;
962
963 It : Iterator := Iterate (All_Diagnostics);
964
965 First : Boolean := True;
966 begin
967 Write_Str ("""" & "results" & """" & ": " & "[");
968 Begin_Block;
969
970 if Present (Diags) then
971 while Has_Next (It) loop
972 Next (It, D);
973
974 if First then
975 First := False;
976 else
977 Write_Char (',');
978 end if;
979
980 NL_And_Indent;
981 Print_Result (D);
982 end loop;
983 end if;
984
985 End_Block;
986 NL_And_Indent;
987 Write_Char (']');
988 end Print_Results;
989
990 ----------------
991 -- Print_Rule --
992 ----------------
993
994 procedure Print_Rule (Diag : Diagnostic_Type) is
995 Human_Id : constant String_Ptr := Get_Human_Id (Diag);
996 begin
997 Write_Char ('{');
998 Begin_Block;
999 NL_And_Indent;
1000
1001 Write_String_Attribute ("id", "[" & To_String (Diag.Id) & "]");
1002 Write_Char (',');
1003 NL_And_Indent;
1004
1005 if Human_Id = null then
1006 Write_String_Attribute ("name", "Uncategorized_Diagnostic");
1007 else
1008 Write_String_Attribute ("name", Human_Id.all);
1009 end if;
1010
1011 End_Block;
1012 NL_And_Indent;
1013 Write_Char ('}');
1014 end Print_Rule;
1015
1016 -----------------
1017 -- Print_Rules --
1018 -----------------
1019
1020 procedure Print_Rules (Diags : Diagnostic_List) is
1021 use Diagnostics.Diagnostics_Lists;
1022
1023 R : Diagnostic_Type;
1024 Rules : constant Diagnostic_List := Get_Unique_Rules (Diags);
1025
1026 It : Iterator := Iterate (Rules);
1027
1028 First : Boolean := True;
1029 begin
1030 Write_Str ("""" & "rules" & """" & ": " & "[");
1031 Begin_Block;
1032
1033 while Has_Next (It) loop
1034 Next (It, R);
1035
1036 if First then
1037 First := False;
1038 else
1039 Write_Char (',');
1040 end if;
1041
1042 NL_And_Indent;
1043 Print_Rule (R);
1044 end loop;
1045
1046 End_Block;
1047 NL_And_Indent;
1048 Write_Char (']');
1049
1050 end Print_Rules;
1051
1052 ----------------
1053 -- Print_Tool --
1054 ----------------
1055
1056 procedure Print_Tool (Diags : Diagnostic_List) is
1057
1058 begin
1059 Write_Str ("""" & "tool" & """" & ": " & "{");
1060 Begin_Block;
1061 NL_And_Indent;
1062
1063 -- -- Attributes of tool
1064
1065 Write_Str ("""" & "driver" & """" & ": " & "{");
1066 Begin_Block;
1067 NL_And_Indent;
1068
1069 -- Attributes of tool.driver
1070
1071 Write_String_Attribute ("name", "GNAT");
1072 Write_Char (',');
1073 NL_And_Indent;
1074
1075 Write_String_Attribute ("version", Gnat_Version_String);
1076 Write_Char (',');
1077 NL_And_Indent;
1078
1079 Print_Rules (Diags);
1080
1081 -- End of tool.driver
1082
1083 End_Block;
1084 NL_And_Indent;
1085
1086 Write_Char ('}');
1087
1088 -- End of tool
1089
1090 End_Block;
1091 NL_And_Indent;
1092
1093 Write_Char ('}');
1094 end Print_Tool;
1095
1096 ----------------
1097 -- Print_Runs --
1098 ----------------
1099
1100 procedure Print_Runs (Diags : Diagnostic_List) is
1101
1102 begin
1103 Write_Str ("""" & "runs" & """" & ": " & "[");
1104 Begin_Block;
1105 NL_And_Indent;
1106
1107 -- Runs can consist of multiple "run"-s. However the GNAT SARIF report
1108 -- only has one.
1109
1110 Write_Char ('{');
1111 Begin_Block;
1112 NL_And_Indent;
1113
1114 -- A run consists of a tool
1115
1116 Print_Tool (Diags);
1117
1118 Write_Char (',');
1119 NL_And_Indent;
1120
1121 -- A run consists of an invocation
1122 Print_Invocations;
1123
1124 Write_Char (',');
1125 NL_And_Indent;
1126
1127 -- A run consists of results
1128
1129 Print_Results (Diags);
1130
1131 -- End of run
1132
1133 End_Block;
1134 NL_And_Indent;
1135
1136 Write_Char ('}');
1137
1138 End_Block;
1139 NL_And_Indent;
1140
1141 -- End of runs
1142
1143 Write_Char (']');
1144 end Print_Runs;
1145
1146 ------------------------
1147 -- Print_SARIF_Report --
1148 ------------------------
1149
1150 procedure Print_SARIF_Report (Diags : Diagnostic_List) is
1151 begin
1152 Write_Char ('{');
1153 Begin_Block;
1154 NL_And_Indent;
1155
1156 Write_String_Attribute ("$schema", SARIF_Schema);
1157 Write_Char (',');
1158 NL_And_Indent;
1159
1160 Write_String_Attribute ("version", SARIF_Version);
1161 Write_Char (',');
1162 NL_And_Indent;
1163
1164 Print_Runs (Diags);
1165
1166 End_Block;
1167 NL_And_Indent;
1168 Write_Char ('}');
1169
1170 Write_Eol;
1171 end Print_SARIF_Report;
1172
1173 end Diagnostics.SARIF_Emitter;