https://gcc.gnu.org/g:b4693ce3a0565bb75d0d5698f2ce2ffc53d1ff84
commit r15-2290-gb4693ce3a0565bb75d0d5698f2ce2ffc53d1ff84 Author: David Malcolm <dmalc...@redhat.com> Date: Wed Jul 24 18:07:55 2024 -0400 diagnostics: SARIF output: add "{start,end}TimeUtc" properties (§§3.20.7-8) gcc/ChangeLog: * diagnostic-format-sarif.cc (make_date_time_string_for_current_time): New. (sarif_invocation::sarif_invocation): Set "startTimeUtc" property (§3.20.7). (sarif_invocation::prepare_to_flush): Set "endTimeUtc" property (§3.20.8). gcc/testsuite/ChangeLog: * c-c++-common/diagnostic-format-sarif-file-1.c: Verify that we have "startTimeUtc" and "endTimeUtc" properties of the correct form. Signed-off-by: David Malcolm <dmalc...@redhat.com> Diff: --- gcc/diagnostic-format-sarif.cc | 28 ++++++++++++++++++++++ .../c-c++-common/diagnostic-format-sarif-file-1.c | 5 ++++ 2 files changed, 33 insertions(+) diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc index 6c7216651627..775d01f75744 100644 --- a/gcc/diagnostic-format-sarif.cc +++ b/gcc/diagnostic-format-sarif.cc @@ -119,6 +119,26 @@ class sarif_tool : public sarif_object {}; class sarif_tool_component : public sarif_object {}; +/* Make a JSON string for the current date and time. + See SARIF v2.1.0 section 3.9 "Date/time properties". + Given that we don't run at the very beginning/end of the + process, it doesn't make sense to be more accurate than + the current second. */ + +static std::unique_ptr<json::string> +make_date_time_string_for_current_time () +{ + time_t t = time (nullptr); + struct tm *tm = gmtime (&t); + char buf[256]; + snprintf (buf, sizeof (buf) - 1, + ("%04i-%02i-%02iT" + "%02i:%02i:%02iZ"), + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + return ::make_unique<json::string> (buf); +} + /* Subclass of sarif_object for SARIF "invocation" objects (SARIF v2.1.0 section 3.20). */ @@ -530,6 +550,10 @@ sarif_invocation::sarif_invocation (sarif_builder &builder, if (const char *pwd = getpwd ()) set<sarif_artifact_location> ("workingDirectory", builder.make_artifact_location_object (pwd)); + + // "startTimeUtc" property (SARIF v2.1.0 section 3.20.7) + set<json::string> ("startTimeUtc", + make_date_time_string_for_current_time ()); } /* Handle an internal compiler error DIAGNOSTIC occurring on CONTEXT. @@ -559,6 +583,10 @@ sarif_invocation::prepare_to_flush (diagnostic_context &context) this object (SARIF v2.1.0 section 3.8) e.g. for recording time vars. */ if (auto client_data_hooks = context.get_client_data_hooks ()) client_data_hooks->add_sarif_invocation_properties (*this); + + // "endTimeUtc" property (SARIF v2.1.0 section 3.20.8); + set<json::string> ("endTimeUtc", + make_date_time_string_for_current_time ()); } /* class sarif_artifact : public sarif_object. */ diff --git a/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-1.c b/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-1.c index c9ad0d238195..fdf602eaae7b 100644 --- a/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-1.c +++ b/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-1.c @@ -37,6 +37,11 @@ 3.20.2 invocation "arguments" property: { dg-final { scan-sarif-file {"arguments": \[} } } + Expect "startTimeUtc" and "endTimeUtc" properties of the form + "nnnn-nn-nnTnn:nn:nnZ" (3.20.7 and 3.20.8): + { dg-final { scan-sarif-file {"startTimeUtc": "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]Z"} } } + { dg-final { scan-sarif-file {"endTimeUtc": "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]Z"} } } + { dg-final { scan-sarif-file {"workingDirectory": } } } { dg-final { scan-sarif-file "\"toolExecutionNotifications\": \\\[\\\]" } } { dg-final { scan-sarif-file "\"executionSuccessful\": true" } }