aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2024-07-24 18:07:55 -0400
committerThomas Koenig <tkoenig@gcc.gnu.org>2024-07-28 19:05:54 +0200
commitc2b6115accb67243e2e1c7ea8f6a5d421435f882 (patch)
tree6f5efbaa11fd000dc12a8a72286a189babd2634f
parentc06d90c59837e570954efe9fbba98560f9b3880a (diff)
downloadgcc-c2b6115accb67243e2e1c7ea8f6a5d421435f882.zip
gcc-c2b6115accb67243e2e1c7ea8f6a5d421435f882.tar.gz
gcc-c2b6115accb67243e2e1c7ea8f6a5d421435f882.tar.bz2
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 <dmalcolm@redhat.com>
-rw-r--r--gcc/diagnostic-format-sarif.cc28
-rw-r--r--gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-1.c5
2 files changed, 33 insertions, 0 deletions
diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc
index 6c72166..775d01f 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 c9ad0d2..fdf602e 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" } }