From patchwork Wed Jan 10 13:41:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 83745 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DE1BA3857B89 for ; Wed, 10 Jan 2024 13:42:01 +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 ESMTPS id 1A43E38582B0 for ; Wed, 10 Jan 2024 13:41:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1A43E38582B0 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 1A43E38582B0 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=1704894078; cv=none; b=u+7+LgqTYJuJMULNLgc5/fMckh2xnnO0diZdM/ldzz7AKy07iyWsiElAEeEvPZH71BsuDluk0ZIsXlj0RmgOVP5UUi+CZzMoS7Zr9X0v/PcmZAmn62VXN4xr/U8/bWmo/dFEFU3EDLpTrUzE+Ci+PY9ZFJwQGd0mI2w9Y7sAXME= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704894078; c=relaxed/simple; bh=/EQ4/LPGL4qwfvc6iOfsF81Uo7YGy+Y9bEDnyO+gBfU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=iJzI2FAB9bGnA2ahIEtFLCJBmQO2PHTZcRY9Vuya/pjjG96qXgRQYS4h9rfBGxEb6nJrxvpks6OUj3U40S2SDxG/jFUSsfN8huJqh/1BtFk4Xmd3XeefR5hXhehoxCTbDQp/kxB+xpJbwWOjgZ1RHnpn5Trf3uyLc0PHthCV36s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704894075; 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=R5gZQVaXk2bnUTOVNqPW/n/4l3VZ7uwJ0ShvMVKjEys=; b=LB7MIJejDe11GKRjWuFK4+Grxsry3TweBEmuJ6jE+F6RM/XbgG8d3C+oaske/jDGFYSn7u whKFLGputqyNuH8CTtbtu/XfsE6X4r9CaRynojdrC6PfQs9gZurhv8CQ9DVwjpHgACBSmB kyKY8PpioVxr1xvU352F7SZY3I4GBeY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-103-23ebBNLuMDWsKDHO_Yq-PQ-1; Wed, 10 Jan 2024 08:41:08 -0500 X-MC-Unique: 23ebBNLuMDWsKDHO_Yq-PQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 5622C831525 for ; Wed, 10 Jan 2024 13:41:08 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.17.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2E1401121306; Wed, 10 Jan 2024 13:41:08 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed 3/3] gcc-urlifier: handle option prefixes such as '-fno-' Date: Wed, 10 Jan 2024 08:41:05 -0500 Message-Id: <20240110134105.749310-3-dmalcolm@redhat.com> In-Reply-To: <20240110134105.749310-1-dmalcolm@redhat.com> References: <20240110134105.749310-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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, 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+patchwork=sourceware.org@gcc.gnu.org Given e.g. this missppelled option (omitting the trailing 's'): $ LANG=C ./xgcc -B. -fno-inline-small-function xgcc: error: unrecognized command-line option '-fno-inline-small-function'; did you mean '-fno-inline-small-functions'? we weren't providing a documentation URL for the suggestion. The issue is the URLification code uses find_opt, which doesn't consider the various '-fno-' prefixes. This patch adds a way to find the pertinent prefix remapping and uses it when determining URLs. With this patch, the suggestion '-fno-inline-small-functions' now gets a documentation link (to that of '-finline-small-functions'). Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-7106-gbe2bf5dc93ca1e. gcc/ChangeLog: * gcc-urlifier.cc (gcc_urlifier::get_url_suffix_for_option): Handle prefix mappings before calling find_opt. (selftest::gcc_urlifier_cc_tests): Add example of urlifying a "-fno-"-prefixed command-line option. * opts-common.cc (get_option_prefix_remapping): New. * opts.h (get_option_prefix_remapping): New decl. Signed-off-by: David Malcolm --- gcc/gcc-urlifier.cc | 49 ++++++++++++++++++++++++++++++++++++++++----- gcc/opts-common.cc | 22 ++++++++++++++++++++ gcc/opts.h | 3 +++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/gcc/gcc-urlifier.cc b/gcc/gcc-urlifier.cc index 6bd176fc2483..be6459e8d7c1 100644 --- a/gcc/gcc-urlifier.cc +++ b/gcc/gcc-urlifier.cc @@ -154,11 +154,46 @@ gcc_urlifier::get_url_suffix_for_option (const char *p, size_t sz) const and skipping the leading '-'. We have a (pointer,size) pair that doesn't necessarily have a - terminator, so create a 0-terminated clone of the string. */ - gcc_assert (sz > 0); - char *tmp = xstrndup (p + 1, sz - 1); // skip the leading '-' - size_t opt = find_opt (tmp, m_lang_mask); - free (tmp); + terminator. + Additionally, we could have one of the e.g. "-Wno-" variants of + the option, which find_opt doesn't handle. + + Hence we need to create input for find_opt in a temporary buffer. */ + char *option_buffer; + + const char *new_prefix; + if (const char *old_prefix = get_option_prefix_remapping (p, sz, &new_prefix)) + { + /* We have one of the variants; generate a buffer containing a copy + that maps from the old prefix to the new prefix + e.g. given "-Wno-suffix", generate "-Wsuffix". */ + gcc_assert (old_prefix[0] == '-'); + gcc_assert (new_prefix); + gcc_assert (new_prefix[0] == '-'); + + const size_t old_prefix_len = strlen (old_prefix); + gcc_assert (old_prefix_len <= sz); + const size_t suffix_len = sz - old_prefix_len; + const size_t new_prefix_len = strlen (new_prefix); + const size_t new_sz = new_prefix_len + suffix_len + 1; + + option_buffer = (char *)xmalloc (new_sz); + memcpy (option_buffer, new_prefix, new_prefix_len); + /* Copy suffix. */ + memcpy (option_buffer + new_prefix_len, p + old_prefix_len, suffix_len); + /* Terminate. */ + option_buffer[new_prefix_len + suffix_len] = '\0'; + } + else + { + /* Otherwise we can simply create a 0-terminated clone of the string. */ + gcc_assert (sz > 0); + gcc_assert (p[0] == '-'); + option_buffer = xstrndup (p, sz); + } + + size_t opt = find_opt (option_buffer + 1, m_lang_mask); + free (option_buffer); if (opt >= N_OPTS) /* Option not recognized. */ @@ -221,6 +256,10 @@ gcc_urlifier_cc_tests () /* Check an option. */ ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("-fpack-struct").get (), "gcc/Code-Gen-Options.html#index-fpack-struct"); + + /* Check a "-fno-" variant of an option. */ + ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("-fno-inline").get (), + "gcc/Optimize-Options.html#index-finline"); } } // namespace selftest diff --git a/gcc/opts-common.cc b/gcc/opts-common.cc index 73126cb74e0e..4a2dff243b0c 100644 --- a/gcc/opts-common.cc +++ b/gcc/opts-common.cc @@ -468,6 +468,28 @@ static const struct option_map option_map[] = { "--no-", NULL, "-f", false, true } }; +/* Given buffer P of size SZ, look for a prefix within OPTION_MAP; + if found, return the prefix and write the new prefix to *OUT_NEW_PREFIX. + Otherwise return nullptr. */ + +const char * +get_option_prefix_remapping (const char *p, size_t sz, + const char **out_new_prefix) +{ + for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++) + { + const char * const old_prefix = option_map[i].opt0; + const size_t old_prefix_len = strlen (old_prefix); + if (old_prefix_len <= sz + && !memcmp (p, old_prefix, old_prefix_len)) + { + *out_new_prefix = option_map[i].new_prefix; + return old_prefix; + } + } + return nullptr; +} + /* Helper function for gcc.cc's driver::suggest_option, for populating the vec of suggestions for misspelled options. diff --git a/gcc/opts.h b/gcc/opts.h index 64f94f63d206..82800b8f87cd 100644 --- a/gcc/opts.h +++ b/gcc/opts.h @@ -491,6 +491,9 @@ extern const struct zero_call_used_regs_opts_s extern vec help_option_arguments; +extern const char *get_option_prefix_remapping (const char *p, size_t sz, + const char **out_new_prefix); + extern void add_misspelling_candidates (auto_vec *candidates, const struct cl_option *option, const char *base_option);