https://gcc.gnu.org/g:d022a068e0c8587b2570147b738cb2781242f162

commit r15-7563-gd022a068e0c8587b2570147b738cb2781242f162
Author: David Malcolm <dmalc...@redhat.com>
Date:   Sat Feb 15 08:17:09 2025 -0500

    sarif-replay: don't add trailing " [error]"
    
    Our SARIF output supplies "error" for the rule ID for DK_ERROR,
    since a value is required, but it's not useful to print in sarif-replay.
    
    Filter it out.
    
    gcc/ChangeLog:
            * libsarifreplay.cc (should_add_rule_p): New.
            (sarif_replayer::handle_result_obj): Use it to filter out rules
            that don't make sense.
    
    gcc/testsuite/ChangeLog:
            * sarif-replay.dg/2.1.0-valid/3.28.6-annotations-1.sarif: Update
            expected output to remove trailing " [error]".
            * sarif-replay.dg/2.1.0-valid/unlabelled-secondary-locations.sarif:
            Likewise.
    
    Signed-off-by: David Malcolm <dmalc...@redhat.com>

Diff:
---
 gcc/libsarifreplay.cc                                 | 19 ++++++++++++++++++-
 .../2.1.0-valid/3.28.6-annotations-1.sarif            |  3 +--
 .../2.1.0-valid/unlabelled-secondary-locations.sarif  |  3 +--
 3 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/gcc/libsarifreplay.cc b/gcc/libsarifreplay.cc
index e2d09088a4a6..21d8e6ce7cf7 100644
--- a/gcc/libsarifreplay.cc
+++ b/gcc/libsarifreplay.cc
@@ -992,6 +992,20 @@ add_any_annotations (libgdiagnostics::diagnostic &diag,
       diag.add_location (annotation.m_phys_loc);
 }
 
+static bool
+should_add_rule_p (const char *rule_id_str, const char *url)
+{
+  if (url)
+    return true;
+
+  /* GCC's sarif output uses "error" for "ruleId", which is already
+     captured in the "level", so don't add a rule for that.  */
+  if (!strcmp (rule_id_str, "error"))
+    return false;
+
+  return true;
+}
+
 /* Process a result object (SARIF v2.1.0 section 3.27).
    Known limitations:
    - doesn't yet handle "ruleIndex" property (§3.27.6)
@@ -1119,7 +1133,10 @@ sarif_replayer::handle_result_obj (const json::object 
&result_obj,
                                                                 prop_help_uri))
            url = url_val->get_string ();
        }
-      err.add_rule (rule_id->get_string (), url);
+
+      const char *rule_id_str = rule_id->get_string ();
+      if (should_add_rule_p (rule_id_str, url))
+       err.add_rule (rule_id_str, url);
     }
   err.set_location (physical_loc);
   err.set_logical_location (logical_loc);
diff --git 
a/gcc/testsuite/sarif-replay.dg/2.1.0-valid/3.28.6-annotations-1.sarif 
b/gcc/testsuite/sarif-replay.dg/2.1.0-valid/3.28.6-annotations-1.sarif
index 4ff6e07ab4d4..fd7e2be44811 100644
--- a/gcc/testsuite/sarif-replay.dg/2.1.0-valid/3.28.6-annotations-1.sarif
+++ b/gcc/testsuite/sarif-replay.dg/2.1.0-valid/3.28.6-annotations-1.sarif
@@ -37,11 +37,10 @@
 /* Verify that we underline and label the ranges for the
    "annotations" above.  */
 /* { dg-begin-multiline-output "" }
-bad-binary-ops-highlight-colors.c:19:23: error: invalid operands to binary + 
(have ‘S’ {aka ‘struct s’} and ‘T’ {aka ‘struct t’}) [error]
+bad-binary-ops-highlight-colors.c:19:23: error: invalid operands to binary + 
(have ‘S’ {aka ‘struct s’} and ‘T’ {aka ‘struct t’})
    19 |   return callee_4a () + callee_4b ();
       |          ~~~~~~~~~~~~ ^ ~~~~~~~~~~~~
       |          |              |
       |          |              T {aka struct t}
       |          S {aka struct s}
    { dg-end-multiline-output "" } */
-// TODO: trailing [error]
diff --git 
a/gcc/testsuite/sarif-replay.dg/2.1.0-valid/unlabelled-secondary-locations.sarif
 
b/gcc/testsuite/sarif-replay.dg/2.1.0-valid/unlabelled-secondary-locations.sarif
index 251e2fd2e786..d462bf8045a9 100644
--- 
a/gcc/testsuite/sarif-replay.dg/2.1.0-valid/unlabelled-secondary-locations.sarif
+++ 
b/gcc/testsuite/sarif-replay.dg/2.1.0-valid/unlabelled-secondary-locations.sarif
@@ -50,11 +50,10 @@
 /* Verify that we underline the "42" here.  */
 /* { dg-begin-multiline-output "" }
 In function 'test_known_fn':
-too-many-arguments.c:5:3: error: too many arguments to function 'fn_a'; 
expected 0, have 1 [error]
+too-many-arguments.c:5:3: error: too many arguments to function 'fn_a'; 
expected 0, have 1
     5 |   fn_a (42);
       |   ^~~~  ~~
 too-many-arguments.c:1:13: note: declared here
     1 | extern void fn_a ();
       |             ^~~~
    { dg-end-multiline-output "" } */
-// TODO: trailing [error]

Reply via email to