From patchwork Wed Jul 24 22:18:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1964567 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=OvfMETR3; 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 4WTpRF2rS9z1yY9 for ; Thu, 25 Jul 2024 08:22:17 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9DD57385DDDF for ; Wed, 24 Jul 2024 22:22:15 +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.129.124]) by sourceware.org (Postfix) with ESMTP id A884C385B510 for ; Wed, 24 Jul 2024 22:18:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A884C385B510 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 A884C385B510 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721859540; cv=none; b=BSaItqufqmJi1m55fBVLsHXmRNvHPa73xTKVtz3MoST5QhpYsjdVIwpAPb+UqvZC4VUa5V0NK5MeaJ98LI0+Kmt8G9iVBEQ0riAhrM259Y64ZwIA2tJhpRbNTWLv/WYmXb/v7NQOclBA9tu7ptShVVqhyjo1HHIQHWAzVgI453Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721859540; c=relaxed/simple; bh=IvToUSoFPNZ75O2f2QKQOJHe69NlNngGWpoQ2lEqhmU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=F7k8Ay9lSaKas5r0EsK2+uWeFa5o0haJvl1p+dg6IJ6FQPOUpsaO9DKr3I/wVT+90J56XY6bOizNIrdCM979SgUMBGJvGmCKFzozgDg445yjd1Ur7O6glTzOVwL6zaLriaS7cWqQhqYb6l7oavAGfT7vetpRRAS+PZwv1zbhwzI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721859536; 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=x3Ml3vDx8sR8WJ8UlopWaXag6CXAyilstz5lQ1P7L7A=; b=OvfMETR3ju5ua7KKQxaiVS+QIk6gxW3jmakIvVXqMotHL8yQ9A9Alt0da0u31rAqsj5AEf zfhHfoOh/BlbWX+REpoFo4BdK65cdBjqvtf61YbqxlbKmXAhLOuQ97qHr/qT73LaPiMfDK 40uSTOLzJ2QQ4w44Z4TjZ9LdB9QIDGs= 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-231-Fc9MrKMSNEORcnVAwQgsgQ-1; Wed, 24 Jul 2024 18:18:55 -0400 X-MC-Unique: Fc9MrKMSNEORcnVAwQgsgQ-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 2376B1955D44 for ; Wed, 24 Jul 2024 22:18:54 +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 47D9A1955F40; Wed, 24 Jul 2024 22:18:53 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 12/16] =?utf-8?q?diagnostics=3A_SARIF_output=3A_add_=22argum?= =?utf-8?q?ents=22_property_=28=C2=A73=2E20=2E2=29?= Date: Wed, 24 Jul 2024 18:18:20 -0400 Message-Id: <20240724221824.585054-13-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, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, 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 gcc/ChangeLog: * diagnostic-format-sarif.cc (sarif_invocation::sarif_invocation): Add "original_argv" param and use it to populate "arguments" property (§3.20.2). (sarif_builder::sarif_builder): Pass argv to m_invocation_obj's ctor. * diagnostic.cc (diagnostic_context::initialize): Initialize m_original_argv. (diagnostic_context::finish): Clean up m_original_argv. (diagnostic_context::set_original_argv): New. * diagnostic.h: Include "unique-argv.h". (diagnostic_context::set_original_argv): New decl. (diagnostic_context::get_original_argv): New decl. (diagnostic_context::m_original_argv): New field. * toplev.cc: Include "unique-argv.h". (general_init): Add "original_argv" param and move it to global_dc. (toplev::main): Stash a copy of the original argv before expansion, and pass it to general_init for use by SARIF output. * unique-argv.h: New file. gcc/jit/ChangeLog: * jit-playback.cc (jit::playback_context::compile) Add a trailing null to argvec. gcc/testsuite/ChangeLog: * c-c++-common/diagnostic-format-sarif-file-1.c: Verify that we have an "arguments" property (§3.20.2). Signed-off-by: David Malcolm --- gcc/diagnostic-format-sarif.cc | 25 +++++-- gcc/diagnostic.cc | 17 +++++ gcc/diagnostic.h | 10 +++ gcc/jit/jit-playback.cc | 6 +- .../diagnostic-format-sarif-file-1.c | 5 ++ gcc/toplev.cc | 13 +++- gcc/unique-argv.h | 67 +++++++++++++++++++ 7 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 gcc/unique-argv.h diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc index 9be84fb268a5..6c7216651627 100644 --- a/gcc/diagnostic-format-sarif.cc +++ b/gcc/diagnostic-format-sarif.cc @@ -125,7 +125,8 @@ class sarif_tool_component : public sarif_object {}; class sarif_invocation : public sarif_object { public: - sarif_invocation (sarif_builder &builder); + sarif_invocation (sarif_builder &builder, + const char * const *original_argv); void add_notification_for_ice (diagnostic_context &context, const diagnostic_info &diagnostic, @@ -329,10 +330,8 @@ public: - GCC supports one-deep nesting of diagnostics (via auto_diagnostic_group), but we only capture location and message information from such nested diagnostics (e.g. we ignore fix-it hints on them) - - doesn't yet capture command-line arguments: would be run.invocations - property (SARIF v2.1.0 section 3.14.11), as invocation objects - (SARIF v2.1.0 section 3.20), but we'd want to capture the arguments to - toplev::main, and the response files. + - although we capture command-line arguments (section 3.20.2), we don't + yet capture response files. - doesn't capture secondary locations within a rich_location (perhaps we should use the "relatedLocations" property: SARIF v2.1.0 section 3.27.22) @@ -513,10 +512,20 @@ sarif_object::get_or_create_properties () /* class sarif_invocation : public sarif_object. */ -sarif_invocation::sarif_invocation (sarif_builder &builder) +sarif_invocation::sarif_invocation (sarif_builder &builder, + const char * const *original_argv) : m_notifications_arr (::make_unique ()), m_success (true) { + // "arguments" property (SARIF v2.1.0 section 3.20.2) + if (original_argv) + { + auto arguments_arr = ::make_unique (); + for (size_t i = 0; original_argv[i]; ++i) + arguments_arr->append_string (original_argv[i]); + set ("arguments", std::move (arguments_arr)); + } + // "workingDirectory" property (SARIF v2.1.0 section 3.20.19) if (const char *pwd = getpwd ()) set ("workingDirectory", @@ -752,7 +761,9 @@ sarif_builder::sarif_builder (diagnostic_context &context, const char *main_input_filename_, bool formatted) : m_context (context), - m_invocation_obj (::make_unique (*this)), + m_invocation_obj + (::make_unique (*this, + context.get_original_argv ())), m_results_array (new json::array ()), m_cur_group_result (nullptr), m_seen_any_relative_paths (false), diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc index aa8afd521fa2..c70c394f7ccd 100644 --- a/gcc/diagnostic.cc +++ b/gcc/diagnostic.cc @@ -261,6 +261,7 @@ diagnostic_context::initialize (int n_opts) m_includes_seen = nullptr; m_client_data_hooks = nullptr; m_diagrams.m_theme = nullptr; + m_original_argv = nullptr; enum diagnostic_text_art_charset text_art_charset = DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI; @@ -385,6 +386,9 @@ diagnostic_context::finish () delete m_urlifier; m_urlifier = nullptr; + + freeargv (m_original_argv); + m_original_argv = nullptr; } void @@ -403,6 +407,19 @@ diagnostic_context::set_client_data_hooks (diagnostic_client_data_hooks *hooks) m_client_data_hooks = hooks; } +void +diagnostic_context::set_original_argv (unique_argv original_argv) +{ + /* Ideally we'd use a unique_argv for m_original_argv, but + diagnostic_context doesn't yet have a ctor/dtor pair. */ + + // Ensure any old value is freed + freeargv (m_original_argv); + + // Take ownership of the new value + m_original_argv = original_argv.release (); +} + void diagnostic_context:: set_option_hooks (diagnostic_option_enabled_cb option_enabled_cb, diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h index 84044b90dfcd..1d83879c50ef 100644 --- a/gcc/diagnostic.h +++ b/gcc/diagnostic.h @@ -21,6 +21,7 @@ along with GCC; see the file COPYING3. If not see #ifndef GCC_DIAGNOSTIC_H #define GCC_DIAGNOSTIC_H +#include "unique-argv.h" #include "rich-location.h" #include "pretty-print.h" #include "diagnostic-core.h" @@ -391,6 +392,12 @@ public: void finish (); + void set_original_argv (unique_argv original_argv); + const char * const *get_original_argv () + { + return const_cast (m_original_argv); + } + void set_set_locations_callback (set_locations_callback_t cb) { m_set_locations_cb = cb; @@ -813,6 +820,9 @@ private: text_art::theme *m_theme; } m_diagrams; + + /* Owned by the context. */ + char **m_original_argv; }; inline void diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc index 1b5445d61013..501d5700873e 100644 --- a/gcc/jit/jit-playback.cc +++ b/gcc/jit/jit-playback.cc @@ -2568,7 +2568,11 @@ compile () if (get_logger ()) for (unsigned i = 0; i < fake_args.length (); i++) get_logger ()->log ("argv[%i]: %s", i, fake_args[i]); - toplev.main (fake_args.length (), + + /* Add a trailing null to argvec; this is not counted in argc. */ + fake_args.safe_push (nullptr); + toplev.main (/* The trailing null is not counted in argv. */ + fake_args.length () - 1, const_cast (fake_args.address ())); exit_scope ("toplev::main"); 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 0a3778323792..c9ad0d238195 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 @@ -32,6 +32,11 @@ { dg-final { scan-sarif-file "\"informationUri\": \"" } } { dg-final { scan-sarif-file "\"invocations\": \\\[" } } + 3.20: "invocation" object: + + 3.20.2 invocation "arguments" property: + { dg-final { scan-sarif-file {"arguments": \[} } } + { dg-final { scan-sarif-file {"workingDirectory": } } } { dg-final { scan-sarif-file "\"toolExecutionNotifications\": \\\[\\\]" } } { dg-final { scan-sarif-file "\"executionSuccessful\": true" } } diff --git a/gcc/toplev.cc b/gcc/toplev.cc index d9e8b34ae7ca..7f19d5c52bd9 100644 --- a/gcc/toplev.cc +++ b/gcc/toplev.cc @@ -93,6 +93,7 @@ along with GCC; see the file COPYING3. If not see #include "ipa-param-manipulation.h" #include "dbgcnt.h" #include "gcc-urlifier.h" +#include "unique-argv.h" #include "selftest.h" @@ -100,7 +101,7 @@ along with GCC; see the file COPYING3. If not see #include #endif -static void general_init (const char *, bool); +static void general_init (const char *, bool, unique_argv original_argv); static void backend_init (void); static int lang_dependent_init (const char *); static void init_asm_output (const char *); @@ -1000,7 +1001,7 @@ internal_error_function (diagnostic_context *, const char *, va_list *) options are parsed. Signal handlers, internationalization etc. ARGV0 is main's argv[0]. */ static void -general_init (const char *argv0, bool init_signals) +general_init (const char *argv0, bool init_signals, unique_argv original_argv) { const char *p; @@ -1028,6 +1029,8 @@ general_init (const char *argv0, bool init_signals) override it later. */ tree_diagnostics_defaults (global_dc); + global_dc->set_original_argv (std::move (original_argv)); + global_dc->m_source_printing.enabled = global_options_init.x_flag_diagnostics_show_caret; global_dc->m_source_printing.show_event_links_p @@ -2241,10 +2244,14 @@ toplev::main (int argc, char **argv) Increase stack size limits if possible. */ stack_limit_increase (64 * 1024 * 1024); + /* Stash a copy of the original argv before expansion + for use by SARIF output. */ + unique_argv original_argv (dupargv (argv)); + expandargv (&argc, &argv); /* Initialization of GCC's environment, and diagnostics. */ - general_init (argv[0], m_init_signals); + general_init (argv[0], m_init_signals, std::move (original_argv)); /* One-off initialization of options that does not need to be repeated when options are added for particular functions. */ diff --git a/gcc/unique-argv.h b/gcc/unique-argv.h new file mode 100644 index 000000000000..41cabac6b762 --- /dev/null +++ b/gcc/unique-argv.h @@ -0,0 +1,67 @@ +/* C++11 wrapper around libiberty's argv.c + Copyright (C) 2024 Free Software Foundation, Inc. + Contributed by David Malcolm + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef GCC_UNIQUE_ARGV_H +#define GCC_UNIQUE_ARGV_H + +/* C++11 wrapper around libiberty's argv.c, with + ownership of the underlying array and strings. */ + +struct unique_argv +{ + /* Take ownership of argv. */ + unique_argv (char **argv) + : m_argv (argv) + { + } + + ~unique_argv () + { + freeargv (m_argv); + } + + unique_argv (const unique_argv &other) = delete; + unique_argv &operator= (const unique_argv &other) = delete; + + unique_argv (unique_argv &&other) + : m_argv (other.m_argv) + { + other.m_argv = nullptr; + } + + unique_argv &operator= (unique_argv &&other) + { + freeargv (m_argv); + m_argv = other.m_argv; + other.m_argv = nullptr; + return *this; + } + + char **release () + { + char **result = m_argv; + m_argv = nullptr; + return result; + } + + char **m_argv; +}; + +#endif /* ! GCC_UNIQUE_ARGV_H */