]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Add Invocation node to the SARIF report
authorViljar Indus <indus@adacore.com>
Fri, 11 Oct 2024 13:34:36 +0000 (16:34 +0300)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Mon, 4 Nov 2024 15:57:57 +0000 (16:57 +0100)
Add an invocation node to the SARIF report that contains the
command line use to activate gnat and whether the execution was
successful or not.

gcc/ada/ChangeLog:

* diagnostics-sarif_emitter.adb (Print_Runs): Add printing for
the invocation node that consists of a single invocations that
is composed of the commandLine and executionSuccessful attributes.

gcc/ada/diagnostics-sarif_emitter.adb

index fe251f9754dbca01f58f4d7e2d8d3a2d107d9475..b6035c2970d659fa8921af10882c6db977a71c1b 100644 (file)
@@ -28,6 +28,10 @@ with Diagnostics.JSON_Utils; use Diagnostics.JSON_Utils;
 with Gnatvsn;                use Gnatvsn;
 with Output;                 use Output;
 with Sinput;                 use Sinput;
+with Lib;                    use Lib;
+with Namet;                  use Namet;
+with Osint;                  use Osint;
+with Errout;                 use Errout;
 
 package body Diagnostics.SARIF_Emitter is
 
@@ -94,6 +98,19 @@ package body Diagnostics.SARIF_Emitter is
    --    ...
    --  ]
 
+   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
    --
@@ -573,6 +590,63 @@ package body Diagnostics.SARIF_Emitter is
       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
+         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 ("""" & "invocations" & """" & ": " & "[");
+      Begin_Block;
+      NL_And_Indent;
+
+      Write_Char ('{');
+      Begin_Block;
+      NL_And_Indent;
+
+      --  Print commandLine
+
+      Write_String_Attribute ("commandLine", Compose_Command_Line);
+      Write_Char (',');
+      NL_And_Indent;
+
+      --  Print executionSuccessful
+
+      Write_String_Attribute
+        ("executionSuccessful",
+         (if Compilation_Errors then "false" else "true"));
+
+      End_Block;
+      NL_And_Indent;
+      Write_Char ('}');
+
+      End_Block;
+      NL_And_Indent;
+      Write_Char (']');
+   end Print_Invocations;
+
    ------------------
    -- Print_Region --
    ------------------
@@ -1052,6 +1126,12 @@ package body Diagnostics.SARIF_Emitter is
       Write_Char (',');
       NL_And_Indent;
 
+      --  A run consists of an invocation
+      Print_Invocations;
+
+      Write_Char (',');
+      NL_And_Indent;
+
       --  A run consists of results
 
       Print_Results (Diags);
@@ -1076,7 +1156,6 @@ package body Diagnostics.SARIF_Emitter is
    ------------------------
 
    procedure Print_SARIF_Report (Diags : Diagnostic_List) is
-
    begin
       Write_Char ('{');
       Begin_Block;