From patchwork Thu Jan 4 14:27:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 185023 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp5643736dyb; Thu, 4 Jan 2024 06:29:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IEpFREwM8tPGhQwNikvoH0HjwYBzmn1oFA6ypjdVKZ/YsD4ZpvE1B+droOhZA3n4oBHgUSL X-Received: by 2002:a05:6870:a70d:b0:204:1df1:8c85 with SMTP id g13-20020a056870a70d00b002041df18c85mr680992oam.94.1704378548505; Thu, 04 Jan 2024 06:29:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704378548; cv=pass; d=google.com; s=arc-20160816; b=m+BRiy1pNcGwTLMAniR4RGcj3g7KaJJ5bWYeiZRp1X7OUKVNxOtQI0wyrSf7nyXFkZ LXPlKGkGtXz8UfOIh7XqkKaK21Rd3R1sWfu2sPdd1E5AlJgF6t7gqNDmRSRNYm6Kbq47 DTg7NfUzO2lABL8NH1KzxQdpVM68fdTW7k6TBD0lcXFhX2KVQQ0vNipoeeithN4RwUmx Qmi49RWNAyr6abZyrOjFzuCX7ENS4IsBA3LLl3cuJJfDehmC85bkl9Cwgnz/pnlg0/vO 6CqeBlaXYbmjtbd79j8xP1oZLYAa9mQv1nUXbQme2TdsfD7W8pgDuM/2n8TRDAjdoa2B alFA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=8prCqqCM/60Y67ZgfO3dD8HukZDhpNIDrFHc9dZzncY=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=Uty0Q3jTDYzVtJdbJLeQnsD2H5u1LznRrZxl+X0DWwpFl5DNIVeFgLJbmXR3wcMkkp UBa4hfTu7RlnjWs45OBtOdFhSpicKc/gR5QZEkY+UP8csHN2k7LegGRKcnpeWfg4SEPE GVACWCeEpoi3u2gq5Ons+5WDYyTS86xbpmohx1Uj/vxSH1rTGt7iP/1Giq61SlRm6WzO dp+Mt+jF8E7jIT8E3NVcWu5bUZ83jCHCKT3NkctP/Ki03hSJrz7kfleg3X/yNk5+7+U3 GiQfl8QUWEY7fvKEvg2Km5oTGqW8UPqSlpZavww7VB40gZdgd3LBkt43VDp73LkF1i5v iKmw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="gKUsr1V/"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id z2-20020a05622a028200b00428226e7640si8187101qtw.297.2024.01.04.06.29.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 06:29:08 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="gKUsr1V/"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 357EA3858006 for ; Thu, 4 Jan 2024 14:29:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id E0E943858015 for ; Thu, 4 Jan 2024 14:27:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E0E943858015 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E0E943858015 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704378454; cv=none; b=gRyfmrwOoO95KL/VNZ81brE7QQpv+DZZ7KBbAl0uNnTjN7VwhXzq7P+6UPpHER/UM5fV5HI7TR0neghfh/4YzdRCAb19+lE1bAlwy9iNXJDpSFgB7FPe5mwZEVLhrqMz+j+SXkJVi4JKG7rmXyCkyY0IGMSMOPCBTYHVXA2uEmA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704378454; c=relaxed/simple; bh=vHL7Nn7EV+Zfx7IrSa3FqlK374YnxETw/dm5sImwZU4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=kh+61/aIw2+e0x3z8ABDo+Al/qSTNei9RSC70JkttW2JtCRQ1/Yz5ikgVbWTjUy/JavX4CT4eC8e28K3lNcjnbG9meBKXkqslyx+HhFLuH65d9UVGxl9gMl3iroiy3UDjx/mwQl1ihxb+EyNbyv1skcHJJLZjt1LAzjQGBmUreY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704378451; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8prCqqCM/60Y67ZgfO3dD8HukZDhpNIDrFHc9dZzncY=; b=gKUsr1V/JY0T+MO0v5gqEvebcenmre6N+PP+p4BrDdXh0f006plm0kbUfOpz1dP5U6hpI7 IWRy8cqmzlUCoKZmnevPt2QXwwvDohD6+inbuuvbf02IcmFVufk8XRFhAcjJk/hKE1mAjU XIe9rNKjx+IoAw5t08PGQy7X1EBnnCI= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-594-uL-m9ZP8PMipS6MBpmS9sg-1; Thu, 04 Jan 2024 09:27:30 -0500 X-MC-Unique: uL-m9ZP8PMipS6MBpmS9sg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B68FC28BBEF5 for ; Thu, 4 Jan 2024 14:27:29 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.17.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8CA02492BC6; Thu, 4 Jan 2024 14:27:29 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: add sarif properties for checker events Date: Thu, 4 Jan 2024 09:27:28 -0500 Message-Id: <20240104142728.427423-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787170440892030718 X-GMAIL-MSGID: 1787170440892030718 As another followup to r14-6057-g12b67d1e13b3cf, optionally add SARIF property bags to threadFlowLocation objects when writing out diagnostic paths, and add analyzer-specific properties to them. This was useful for debugging PR analyzer/112790. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu. Pushed to trunk as r14-6919-g05c99b1c7965f4. gcc/analyzer/ChangeLog: * checker-event.cc: Include "diagnostic-format-sarif.h" and "tree-logical-location.h". (checker_event::maybe_add_sarif_properties): New. (superedge_event::maybe_add_sarif_properties): New. (superedge_event::superedge_event): Add comment. * checker-event.h (checker_event::maybe_add_sarif_properties): New decl. (superedge_event::maybe_add_sarif_properties): New decl. gcc/ChangeLog: * diagnostic-format-sarif.cc (sarif_builder::make_logical_location_object): Convert to... (make_sarif_logical_location_object): ...this. (sarif_builder::set_any_logical_locs_arr): Update for above change. (sarif_builder::make_thread_flow_location_object): Call maybe_add_sarif_properties on each diagnostic_event. * diagnostic-format-sarif.h (class logical_location): New forward decl. (make_sarif_logical_location_object): New decl. * diagnostic-path.h (class sarif_object): New forward decl. (diagnostic_event::maybe_add_sarif_properties): New vfunc. Signed-off-by: David Malcolm --- gcc/analyzer/checker-event.cc | 43 ++++++++++++++++++++++++++++++++++ gcc/analyzer/checker-event.h | 6 +++++ gcc/diagnostic-format-sarif.cc | 13 +++++----- gcc/diagnostic-format-sarif.h | 5 ++++ gcc/diagnostic-path.h | 9 +++++++ 5 files changed, 70 insertions(+), 6 deletions(-) diff --git a/gcc/analyzer/checker-event.cc b/gcc/analyzer/checker-event.cc index 3cb2fb9175cd..8ce35f22e20e 100644 --- a/gcc/analyzer/checker-event.cc +++ b/gcc/analyzer/checker-event.cc @@ -55,6 +55,8 @@ along with GCC; see the file COPYING3. If not see #include "analyzer/constraint-manager.h" #include "analyzer/checker-event.h" #include "analyzer/exploded-graph.h" +#include "diagnostic-format-sarif.h" +#include "tree-logical-location.h" #if ENABLE_ANALYZER @@ -142,6 +144,30 @@ checker_event::get_meaning () const return meaning (); } +/* Implementation of diagnostic_event::maybe_add_sarif_properties + for checker_event. */ + +void +checker_event:: +maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj) const +{ + sarif_property_bag &props = thread_flow_loc_obj.get_or_create_properties (); +#define PROPERTY_PREFIX "gcc/analyzer/checker_event/" + props.set (PROPERTY_PREFIX "emission_id", + diagnostic_event_id_to_json (m_emission_id)); + props.set_string (PROPERTY_PREFIX "kind", event_kind_to_string (m_kind)); + + if (m_original_fndecl != m_effective_fndecl) + { + tree_logical_location logical_loc (m_original_fndecl); + props.set (PROPERTY_PREFIX "original_fndecl", + make_sarif_logical_location_object (logical_loc)); + } + if (m_original_depth != m_effective_depth) + props.set_integer (PROPERTY_PREFIX "original_depth", m_original_depth); +#undef PROPERTY_PREFIX +} + /* Dump this event to PP (for debugging/logging purposes). */ void @@ -498,6 +524,21 @@ state_change_event::get_meaning () const /* class superedge_event : public checker_event. */ +/* Implementation of diagnostic_event::maybe_add_sarif_properties + for superedge_event. */ + +void +superedge_event::maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj) + const +{ + checker_event::maybe_add_sarif_properties (thread_flow_loc_obj); + sarif_property_bag &props = thread_flow_loc_obj.get_or_create_properties (); +#define PROPERTY_PREFIX "gcc/analyzer/superedge_event/" + if (m_sedge) + props.set (PROPERTY_PREFIX "superedge", m_sedge->to_json ()); +#undef PROPERTY_PREFIX +} + /* Get the callgraph_superedge for this superedge_event, which must be for an interprocedural edge, rather than a CFG edge. */ @@ -548,6 +589,8 @@ superedge_event::superedge_event (enum event_kind kind, m_eedge (eedge), m_sedge (eedge.m_sedge), m_var (NULL_TREE), m_critical_state (0) { + /* Note that m_sedge can be nullptr for e.g. jumps through + function pointers. */ } /* class cfg_edge_event : public superedge_event. */ diff --git a/gcc/analyzer/checker-event.h b/gcc/analyzer/checker-event.h index dcb2e27faa60..72ee103076c8 100644 --- a/gcc/analyzer/checker-event.h +++ b/gcc/analyzer/checker-event.h @@ -118,6 +118,9 @@ public: return 0; } + void + maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj) const override; + /* Additional functionality. */ int get_original_stack_depth () const { return m_original_depth; } @@ -391,6 +394,9 @@ public: class superedge_event : public checker_event { public: + void maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj) + const override; + /* Mark this edge event as being either an interprocedural call or return in which VAR is in STATE, and that this is critical to the diagnostic (so that get_desc can attempt to get a better description diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc index 05b2c6df2e27..7b903bc90c35 100644 --- a/gcc/diagnostic-format-sarif.cc +++ b/gcc/diagnostic-format-sarif.cc @@ -184,8 +184,6 @@ private: void set_any_logical_locs_arr (json::object *location_obj, const logical_location *logical_loc); json::object *make_location_object (const diagnostic_event &event); - json::object * - make_logical_location_object (const logical_location &logical_loc) const; json::object *make_code_flow_object (const diagnostic_path &path); json::object * make_thread_flow_location_object (const diagnostic_event &event, @@ -754,7 +752,7 @@ set_any_logical_locs_arr (json::object *location_obj, { if (!logical_loc) return; - json::object *logical_loc_obj = make_logical_location_object (*logical_loc); + json::object *logical_loc_obj = make_sarif_logical_location_object (*logical_loc); json::array *location_locs_arr = new json::array (); location_locs_arr->append (logical_loc_obj); location_obj->set ("logicalLocations", location_locs_arr); @@ -1092,8 +1090,7 @@ maybe_get_sarif_kind (enum logical_location_kind kind) or return NULL. */ json::object * -sarif_builder:: -make_logical_location_object (const logical_location &logical_loc) const +make_sarif_logical_location_object (const logical_location &logical_loc) { json::object *logical_loc_obj = new json::object (); @@ -1163,7 +1160,11 @@ json::object * sarif_builder::make_thread_flow_location_object (const diagnostic_event &ev, int path_event_idx) { - json::object *thread_flow_loc_obj = new json::object (); + sarif_object *thread_flow_loc_obj = new sarif_object (); + + /* Give diagnostic_event subclasses a chance to add custom properties + via a property bag. */ + ev.maybe_add_sarif_properties (*thread_flow_loc_obj); /* "location" property (SARIF v2.1.0 section 3.38.3). */ json::object *location_obj = make_location_object (ev); diff --git a/gcc/diagnostic-format-sarif.h b/gcc/diagnostic-format-sarif.h index 82ed9b9ee440..485564e14214 100644 --- a/gcc/diagnostic-format-sarif.h +++ b/gcc/diagnostic-format-sarif.h @@ -23,6 +23,8 @@ along with GCC; see the file COPYING3. If not see #include "json.h" +class logical_location; + /* Concrete subclass of json::object for SARIF property bags (SARIF v2.1.0 section 3.8). */ @@ -42,4 +44,7 @@ public: sarif_property_bag &get_or_create_properties (); }; +extern json::object * +make_sarif_logical_location_object (const logical_location &logical_loc); + #endif /* ! GCC_DIAGNOSTIC_FORMAT_SARIF_H */ diff --git a/gcc/diagnostic-path.h b/gcc/diagnostic-path.h index d39872abb9f6..d24fb50b8d80 100644 --- a/gcc/diagnostic-path.h +++ b/gcc/diagnostic-path.h @@ -24,6 +24,8 @@ along with GCC; see the file COPYING3. If not see #include "diagnostic.h" /* for ATTRIBUTE_GCC_DIAG. */ #include "diagnostic-event-id.h" +class sarif_object; + /* A diagnostic_path is an optional additional piece of metadata associated with a diagnostic (via its rich_location). @@ -157,6 +159,13 @@ class diagnostic_event virtual meaning get_meaning () const = 0; virtual diagnostic_thread_id_t get_thread_id () const = 0; + + /* Hook for SARIF output to allow for adding diagnostic-specific + properties to the threadFlowLocation object's property bag. */ + virtual void + maybe_add_sarif_properties (sarif_object &/*thread_flow_loc_obj*/) const + { + } }; /* Abstract base class representing a thread of execution within