From patchwork Wed Jul 24 22:18:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1964568 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QrSKH0dL; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WTpRv4n86z1yY9 for ; Thu, 25 Jul 2024 08:22:51 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DE878385DDDD for ; Wed, 24 Jul 2024 22:22:49 +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 ESMTP id 9036F385C6C6 for ; Wed, 24 Jul 2024 22:18:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9036F385C6C6 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 9036F385C6C6 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=1721859541; cv=none; b=rvh719epKLuSMdhm0EtEuz79mRKC0oyiz7VcbgP4r/7b4lbVQCqdp95Onx9oP/POettiWfYqxzkRVkvnndOhiO8Qnlp+2FA4KyVctvKcVZAx1pJ8ylGFWQ2rax8o0WaOR26v5GaqVnGOpWzL6xLixtSxVeqRjUDcCGWfpWLrIww= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721859541; c=relaxed/simple; bh=eoAQziLB1NyiU6myxLLtdi48CTcFIXXmpEHe7d2Mh8k=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=r93FfHUvVSROB/aNqIRRQaQHUf+eY4gsZlEqv/yspazySmaNcuk8c1emss6QxWU1mO6oNhx7tSpGAJf2702pzuPfrxUaZc1JsbFDC1TR76RUBfxXz+A356EwhmT2XKIw/LvkaErf6LJ/TIepxaGYiI2bkhy7SFvV/URyMda53l4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721859539; 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: in-reply-to:in-reply-to:references:references; bh=JkE39pLWXgS3YJZi+cnnDYoNt9SwktQIVZR8WUAFub0=; b=QrSKH0dLPOE3E2p4eQEYIhoAIFOYXTjK/ij/wE9axTekyC/ywM1I4vhuC781nOmqjJxgAh 905RYCdS5Bdoj+xyLC/Kbji8aCU4lS6ah5QfOGMOZrmebo/TKEMLCv3xAWHPTnIb7YD16L RZJlF73vC0ad0rpQj3+HA67oeXxGNl4= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-584-3a-p0AYlOMCYOOph-CWPUg-1; Wed, 24 Jul 2024 18:18:57 -0400 X-MC-Unique: 3a-p0AYlOMCYOOph-CWPUg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 073081955D45 for ; Wed, 24 Jul 2024 22:18:57 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.33.183]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id BB0911955F40; Wed, 24 Jul 2024 22:18:55 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 14/16] =?utf-8?q?diagnostics=3A_SARIF_output=3A_add_=22annot?= =?utf-8?q?ations=22_property_=28=C2=A73=2E28=2E6=29?= Date: Wed, 24 Jul 2024 18:18:22 -0400 Message-Id: <20240724221824.585054-15-dmalcolm@redhat.com> In-Reply-To: <20240724221824.585054-1-dmalcolm@redhat.com> References: <20240724221824.585054-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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~incoming=patchwork.ozlabs.org@gcc.gnu.org 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 --- gcc/diagnostic-format-sarif.cc | 70 ++++++++++++++++++- gcc/json.h | 3 + gcc/selftest-json.cc | 16 +++++ gcc/selftest-json.h | 14 ++++ ...diagnostic-format-sarif-file-Wbidi-chars.c | 8 +++ 5 files changed, 110 insertions(+), 1 deletion(-) diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc index 775d01f75744..afb29eab5839 100644 --- a/gcc/diagnostic-format-sarif.cc +++ b/gcc/diagnostic-format-sarif.cc @@ -345,6 +345,7 @@ public: - CWE metadata - diagnostic groups (see limitations below) - logical locations (e.g. cfun) + - labelled ranges (as annotations) Known limitations: - GCC supports one-deep nesting of diagnostics (via auto_diagnostic_group), @@ -361,7 +362,6 @@ public: ("artifact.hashes" property (SARIF v2.1.0 section 3.24.11). - doesn't capture the "analysisTarget" property (SARIF v2.1.0 section 3.27.13). - - doesn't capture labelled ranges - doesn't capture -Werror cleanly - doesn't capture inlining information (can SARIF handle this?) - doesn't capture macro expansion information (can SARIF handle this?). */ @@ -1210,6 +1210,38 @@ sarif_builder::make_location_object (const rich_location &rich_loc, /* "logicalLocations" property (SARIF v2.1.0 section 3.28.4). */ set_any_logical_locs_arr (*location_obj, logical_loc); + /* "annotations" property (SARIF v2.1.0 section 3.28.6). */ + { + /* Create annotations for any labelled ranges. */ + std::unique_ptr annotations_arr = nullptr; + for (unsigned int i = 0; i < rich_loc.get_num_locations (); i++) + { + const location_range *range = rich_loc.get_range (i); + if (const range_label *label = range->m_label) + { + label_text text = label->get_text (i); + if (text.get ()) + { + location_t range_loc = rich_loc.get_loc (i); + auto region + = maybe_make_region_object (range_loc, + rich_loc.get_column_override ()); + if (region) + { + if (!annotations_arr) + annotations_arr = ::make_unique (); + region->set + ("message", make_message_object (text.get ())); + annotations_arr->append (std::move (region)); + } + } + } + } + if (annotations_arr) + location_obj->set ("annotations", + std::move (annotations_arr)); + } + /* A flag for hinting that the diagnostic involves issues at the level of character encodings (such as homoglyphs, or misleading bidirectional control codes), and thus that it will be helpful @@ -2416,6 +2448,9 @@ test_make_location_object (const line_table_case &case_) sarif_builder builder (dc, "MAIN_INPUT_FILENAME", true); + /* These "columns" are byte offsets, whereas later on the columns + in the generated SARIF use sarif_builder::get_sarif_column and + thus respect tabs, encoding. */ const location_t foo = make_location (linemap_position_for_column (line_table, 1), linemap_position_for_column (line_table, 1), @@ -2480,6 +2515,39 @@ test_make_location_object (const line_table_case &case_) } } } + auto annotations + = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (location_obj.get (), + "annotations"); + ASSERT_EQ (annotations->size (), 3); + { + { + auto a0 = (*annotations)[0]; + ASSERT_JSON_INT_PROPERTY_EQ (a0, "startLine", 1); + ASSERT_JSON_INT_PROPERTY_EQ (a0, "startColumn", 1); + ASSERT_JSON_INT_PROPERTY_EQ (a0, "endColumn", 7); + auto message + = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (a0, "message"); + ASSERT_JSON_STRING_PROPERTY_EQ (message, "text", "label0"); + } + { + auto a1 = (*annotations)[1]; + ASSERT_JSON_INT_PROPERTY_EQ (a1, "startLine", 1); + ASSERT_JSON_INT_PROPERTY_EQ (a1, "startColumn", 10); + ASSERT_JSON_INT_PROPERTY_EQ (a1, "endColumn", 15); + auto message + = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (a1, "message"); + ASSERT_JSON_STRING_PROPERTY_EQ (message, "text", "label1"); + } + { + auto a2 = (*annotations)[2]; + ASSERT_JSON_INT_PROPERTY_EQ (a2, "startLine", 1); + ASSERT_JSON_INT_PROPERTY_EQ (a2, "startColumn", 16); + ASSERT_JSON_INT_PROPERTY_EQ (a2, "endColumn", 25); + auto message + = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (a2, "message"); + ASSERT_JSON_STRING_PROPERTY_EQ (message, "text", "label2"); + } + } } /* Run all of the selftests within this file. */ diff --git a/gcc/json.h b/gcc/json.h index f80a5e82caf3..96721edf5365 100644 --- a/gcc/json.h +++ b/gcc/json.h @@ -170,6 +170,9 @@ class array : public value append (v.release ()); } + size_t size () const { return m_elements.length (); } + value *operator[] (size_t i) const { return m_elements[i]; } + private: auto_vec m_elements; }; diff --git a/gcc/selftest-json.cc b/gcc/selftest-json.cc index 86f27cb82999..271e9b441120 100644 --- a/gcc/selftest-json.cc +++ b/gcc/selftest-json.cc @@ -96,6 +96,22 @@ expect_json_object_with_object_property (const location &loc, return static_cast (property_value); } +/* Assert that VALUE is a non-null json::object that has property + PROPERTY_NAME, and that the property value is a non-null JSON array. + Return the value of the property as a json::array. + Use LOC for any failures. */ + +const json::array * +expect_json_object_with_array_property (const location &loc, + const json::value *value, + const char *property_name) +{ + const json::value *property_value + = expect_json_object_with_property (loc, value, property_name); + ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_ARRAY); + return static_cast (property_value); +} + /* Assert that VALUE is a non-null json::object that has property PROPERTY_NAME, and that the value of that property is a non-null JSON string equalling EXPECTED_VALUE. diff --git a/gcc/selftest-json.h b/gcc/selftest-json.h index 75a20d519a4c..23b4d18951ca 100644 --- a/gcc/selftest-json.h +++ b/gcc/selftest-json.h @@ -77,6 +77,20 @@ expect_json_object_with_object_property (const location &loc, (JSON_VALUE), \ (PROPERTY_NAME)) +/* Assert that VALUE is a non-null json::object that has property + PROPERTY_NAME, and that the property value is a non-null JSON array. + Return the value of the property as a json::array. + Use LOC for any failures. */ + +const json::array * +expect_json_object_with_array_property (const location &loc, + const json::value *value, + const char *property_name); +#define EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY(JSON_VALUE, PROPERTY_NAME) \ + expect_json_object_with_array_property ((SELFTEST_LOCATION), \ + (JSON_VALUE), \ + (PROPERTY_NAME)) + /* Assert that VALUE is a non-null json::object that has property PROPERTY_NAME, and that the value of that property is a non-null JSON string equalling EXPECTED_VALUE. diff --git a/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-Wbidi-chars.c b/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-Wbidi-chars.c index 8a287d6c8683..f6084ad04a6f 100644 --- a/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-Wbidi-chars.c +++ b/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-Wbidi-chars.c @@ -29,4 +29,12 @@ int main() { { dg-final { scan-sarif-file {"rendered": } } } + Verify that we have an "annotations" property for the + labelled ranges (3.28.6). + { dg-final { scan-sarif-file {"annotations": } } } + and that the annotations capture the labels as messages, + using "." in place of awkard characters: + { dg-final { scan-sarif-file {"message": ."text": "end of bidirectional context"} } } + { dg-final { scan-sarif-file {"message": ."text": "U.202E .RIGHT-TO-LEFT OVERRIDE."} } } + { dg-final { scan-sarif-file {"message": ."text": "U.2066 .LEFT-TO-RIGHT ISOLATE."} } } */