diff mbox series

[COMMITTED,20/38] ada: Add Invocation node to the SARIF report

Message ID 20241104161116.1431659-20-poulhies@adacore.com
State New
Headers show
Series [COMMITTED,01/38] ada: Fix asymmetry in resolution of unary intrinsic operators | expand

Commit Message

Marc Poulhiès Nov. 4, 2024, 4:10 p.m. UTC
From: Viljar Indus <indus@adacore.com>

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.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/diagnostics-sarif_emitter.adb | 81 ++++++++++++++++++++++++++-
 1 file changed, 80 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/gcc/ada/diagnostics-sarif_emitter.adb b/gcc/ada/diagnostics-sarif_emitter.adb
index fe251f9754d..b6035c2970d 100644
--- a/gcc/ada/diagnostics-sarif_emitter.adb
+++ b/gcc/ada/diagnostics-sarif_emitter.adb
@@ -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;