]> git.ipfire.org Git - thirdparty/gcc.git/commit
diagnostics: SARIF output: add "annotations" property (§3.28.6)
authorDavid Malcolm <dmalcolm@redhat.com>
Wed, 24 Jul 2024 22:07:56 +0000 (18:07 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Wed, 24 Jul 2024 22:07:56 +0000 (18:07 -0400)
commitd7a688fc960f78c62aacdc5acb8432873fed300e
treee67ed958b9050e2969c915b84677156e210b0772
parentb4693ce3a0565bb75d0d5698f2ce2ffc53d1ff84
diagnostics: SARIF output: add "annotations" property (§3.28.6)

This patch extends our SARIF output so that if a diagnostic has any
labelled source ranges, the "location" object gains an "annotations"
property capturing them (§3.28.6).

For example, given this textual output:

../../src/gcc/testsuite/gcc.dg/bad-binary-ops.c: In function ‘test_2’:
../../src/gcc/testsuite/gcc.dg/bad-binary-ops.c:31:11: error: invalid operands to binary + (have ‘struct s’ and ‘struct t’)
   30 |   return (some_function ()
      |           ~~~~~~~~~~~~~~~~
      |           |
      |           struct s
   31 |           + some_other_function ());
      |           ^ ~~~~~~~~~~~~~~~~~~~~~~
      |             |
      |             struct t

the SARIF output gains this within the result's location[0]:

   "annotations": [{"startLine": 30,
                    "startColumn": 11,
                    "endColumn": 27,
                    "message": {"text": "struct s"}},
                   {"startLine": 31,
                    "startColumn": 13,
                    "endColumn": 35,
                    "message": {"text": "struct t"}}]}]},

gcc/ChangeLog:
* diagnostic-format-sarif.cc
(sarif_builder::make_location_object): Add "annotations" property if
there are any labelled ranges (§3.28.6).
(selftest::test_make_location_object): Verify annotations are added
to location_obj.
* json.h (json::array::size): New.
(json::array::operator[]): New.
* selftest-json.cc
(selftest::expect_json_object_with_array_property): New.
* selftest-json.h
(selftest::expect_json_object_with_array_property): New decl.
(EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY): New macro.

gcc/testsuite/ChangeLog:
* c-c++-common/diagnostic-format-sarif-file-Wbidi-chars.c: Verify
that we have an "annotations" property for the labelled
ranges (§3.28.6).

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/diagnostic-format-sarif.cc
gcc/json.h
gcc/selftest-json.cc
gcc/selftest-json.h
gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-Wbidi-chars.c