]> git.ipfire.org Git - thirdparty/gcc.git/commit
SARIF and -ftime-report's output [PR109361]
authorDavid Malcolm <dmalcolm@redhat.com>
Mon, 31 Jul 2023 15:13:02 +0000 (11:13 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Mon, 31 Jul 2023 15:13:02 +0000 (11:13 -0400)
commit75d623946d4b6ea80a777b789b116d4b4a2298dc
tree09fd7297586bd0c2b71502e834430b9441d4e2ae
parenta104e9ac0ae9a7e78ec2edd0b81074946646a87d
SARIF and -ftime-report's output [PR109361]

This patch adds support for embeddding profiling information about the
compiler itself into the SARIF output.

Specifically, if SARIF diagnostic output is requested, via
-fdiagnostics-format=sarif-file or -fdiagnostics-format=sarif-stderr,
then any -ftime-report output is written in JSON form into the SARIF
output, rather than to stderr.

In earlier versions of this patch I extended -ftime-report so that
*as well* as writing to stderr, it would embed the information in any
SARIF output.  This turned out to be awkward to use, in that I found
myself needing to get the data in JSON form without also having it
emitted on stderr (which was fouling my build scripts).

The timing information is written to the SARIF as a "gcc/timeReport"
property within a property bag of the "invocation" object.

Here's an example of the output:

  "invocations": [
      {
          "executionSuccessful": true,
          "toolExecutionNotifications": [],
          "properties": {
              "gcc/timeReport": {
                  "timevars": [
                      {
                          "name": "phase setup",
                          "elapsed": {
                              "user": 0.04,
                              "sys": 0,
                              "wall": 0.04,
                              "ggc_mem": 1863472
                          }
                      },

                      [...snip...]

                      {
                          "name": "analyzer: processing worklist",
                          "elapsed": {
                              "user": 0.06,
                              "sys": 0,
                              "wall": 0.06,
                              "ggc_mem": 48
                          }
                      },
                      {
                          "name": "analyzer: emitting diagnostics",
                          "elapsed": {
                              "user": 0.01,
                              "sys": 0,
                              "wall": 0.01,
                              "ggc_mem": 0
                          }
                      },
                      {
                          "name": "TOTAL",
                          "elapsed": {
                              "user": 0.21,
                              "sys": 0.03,
                              "wall": 0.24,
                              "ggc_mem": 3368736
                          }
                      }
                  ],
                  "CHECKING_P": true,
                  "flag_checking": true
              }
          }
      }
  ]

The documentation notes that the precise output format is subject
to change.

I have successfully used this in my analyzer integration tests to get
timing information about which source files get slowed down by the
analyzer.  I've validated the generated .sarif files against the SARIF
schema.

gcc/ChangeLog:
PR analyzer/109361
* diagnostic-client-data-hooks.h (class sarif_object): New forward
decl.
(diagnostic_client_data_hooks::add_sarif_invocation_properties):
New vfunc.
* diagnostic-format-sarif.cc: Include "diagnostic-format-sarif.h".
(class sarif_invocation): Inherit from sarif_object rather than
json::object.
(class sarif_result): Likewise.
(class sarif_ice_notification): Likewise.
(sarif_object::get_or_create_properties): New.
(sarif_invocation::prepare_to_flush): Add "context" param.  Use it
to call the context's add_sarif_invocation_properties hook.
(sarif_builder::flush_to_file): Pass m_context to
sarif_invocation::prepare_to_flush.
* diagnostic-format-sarif.h: New header.
* doc/invoke.texi (Developer Options): Clarify that -ftime-report
writes to stderr.  Document that if SARIF diagnostic output is
requested then any timing information is written in JSON form as
part of the SARIF output, rather than to stderr.
* timevar.cc: Include "json.h".
(timer::named_items::m_hash_map): Split out type into...
(timer::named_items::hash_map_t): ...this new typedef.
(timer::named_items::make_json): New function.
(timevar_diff): New function.
(make_json_for_timevar_time_def): New function.
(timer::timevar_def::make_json): New function.
(timer::make_json): New function.
* timevar.h (class json::value): New forward decl.
(timer::make_json): New decl.
(timer::timevar_def::make_json): New decl.
* tree-diagnostic-client-data-hooks.cc: Include
"diagnostic-format-sarif.h" and "timevar.h".
(compiler_data_hooks::add_sarif_invocation_properties): New vfunc
implementation.

gcc/testsuite/ChangeLog:
PR analyzer/109361
* c-c++-common/diagnostic-format-sarif-file-timevars-1.c: New test.
* c-c++-common/diagnostic-format-sarif-file-timevars-2.c: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/diagnostic-client-data-hooks.h
gcc/diagnostic-format-sarif.cc
gcc/diagnostic-format-sarif.h [new file with mode: 0644]
gcc/doc/invoke.texi
gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-timevars-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-timevars-2.c [new file with mode: 0644]
gcc/timevar.cc
gcc/timevar.h
gcc/tree-diagnostic-client-data-hooks.cc