From patchwork Wed Nov 22 15:36:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 840443 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-467707-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="GCnzg/kz"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yhmjX0GQHz9s76 for ; Thu, 23 Nov 2017 02:34:54 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; q=dns; s=default; b=YgJLJPPj6DBc UfwzBIS/PvYLAb/7L1+iIPRKbSv4GIeHMiOjMsnPcE6q947jYEbq1wUGmCQMBI69 ATsfanmHt6w04wdJRpKL16tSKSr6ib1XhWk02sQmqD4JfzQyC51QXF9zuVkTLlOf 6Pe2480BOMBDHPiqG3ADYyMg8xArE7M= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; s=default; bh=X1v7GjTZ4yhBLnEb2r yAu5jUpXg=; b=GCnzg/kzkvL1jk9EUXOPb8Q06K7mPi/CFjZY4u8CtgqC7XMp9F sfjkewo+AYcd//qaq0XI5dOMzinPBmUiMDvEo8kcJVcNgTDT1tA94KI+Di6RKFsN IJCF4dmyZzYN6aJearUhHuLICCPEVNyovt4R/wUhtQV5PZ2TRQxl4u4Cw= Received: (qmail 21360 invoked by alias); 22 Nov 2017 15:34:31 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 21322 invoked by uid 89); 22 Nov 2017 15:34:31 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KB_WAM_FROM_NAME_SINGLEWORD, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=offer X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 22 Nov 2017 15:34:29 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AE84261D1D for ; Wed, 22 Nov 2017 15:34:28 +0000 (UTC) Received: from c64.redhat.com (ovpn-112-13.phx2.redhat.com [10.3.112.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id 837446001A; Wed, 22 Nov 2017 15:34:27 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH] C++: avoid most reserved words as misspelling suggestions (PR c++/81610 and PR c++/80567) Date: Wed, 22 Nov 2017 10:36:47 -0500 Message-Id: <1511365007-22684-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes lookup_name_fuzzy can offer some reserved words as suggestions for misspelled words, helping with "singed"/"signed" typos. PR c++/81610 and PR c++/80567 report problems where the C++ frontend suggested "if", "for" and "else" as corrections for misspelled variable names. The root cause is that in r247233 ("Fix spelling suggestions for reserved words (PR c++/80177)") I loosened the conditions on these reserved words, adding this condition: if (kind == FUZZY_LOOKUP_TYPENAME) to the logic for rejecting words that don't start decl-specifiers, to allow for "static_assert" to be offered. This is too loose a condition: we don't want to suggest *any* reserved word when we're in a context where we don't know we expect a typename. For the kinds of error-recover situations where we're suggesting spelling corrections we don't have much contextual information, so it seems prudent to be stricter about which reserved words we offer as spelling suggestions; I don't think it makes sense for us to suggest e.g. "for". This patch implements that by effectively reinstating the old logic, but special-casing RID_STATIC_ASSERT, moving the logic to a new subroutine (in case we want to allow for other special-cases). I attempted to add suggestions for the various RID_*CAST, to cope with e.g. "reinterptet_cast" (I can never type that correctly on the first try), but the following '<' token confuses the error-recovery enough that the suggestion code isn't triggered. Hence this more minimal fix. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. OK for trunk? gcc/cp/ChangeLog: PR c++/81610 PR c++/80567 * name-lookup.c (suggest_rid_p): New function. (lookup_name_fuzzy): Replace enum-rid-filtering logic with call to suggest_rid_p. gcc/testsuite/ChangeLog: PR c++/81610 PR c++/80567 * g++.dg/spellcheck-reswords.C: New test case. * g++.dg/spellcheck-stdlib.C: Remove xfail from dg-bogus suggestion of "if". --- gcc/cp/name-lookup.c | 31 +++++++++++++++++++++++++++--- gcc/testsuite/g++.dg/spellcheck-reswords.C | 11 +++++++++++ gcc/testsuite/g++.dg/spellcheck-stdlib.C | 2 +- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/spellcheck-reswords.C diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index 7c363b0..a96be46 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -5671,6 +5671,32 @@ class macro_use_before_def : public deferred_diagnostic cpp_hashnode *m_macro; }; +/* Determine if it can ever make sense to offer RID as a suggestion for + a misspelling. + + Subroutine of lookup_name_fuzzy. */ + +static bool +suggest_rid_p (enum rid rid) +{ + switch (rid) + { + /* Support suggesting function-like keywords. */ + case RID_STATIC_ASSERT: + return true; + + default: + /* Support suggesting the various decl-specifier words, to handle + e.g. "singed" vs "signed" typos. */ + if (cp_keyword_starts_decl_specifier_p (rid)) + return true; + + /* Otherwise, don't offer it. This avoids suggesting e.g. "if" + and "do" for short misspellings, which are likely to lead to + nonsensical results. */ + return false; + } +} /* Search for near-matches for NAME within the current bindings, and within macro names, returning the best match as a const char *, or NULL if @@ -5735,9 +5761,8 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) { const c_common_resword *resword = &c_common_reswords[i]; - if (kind == FUZZY_LOOKUP_TYPENAME) - if (!cp_keyword_starts_decl_specifier_p (resword->rid)) - continue; + if (!suggest_rid_p (resword->rid)) + continue; tree resword_identifier = ridpointers [resword->rid]; if (!resword_identifier) diff --git a/gcc/testsuite/g++.dg/spellcheck-reswords.C b/gcc/testsuite/g++.dg/spellcheck-reswords.C new file mode 100644 index 0000000..db6104b --- /dev/null +++ b/gcc/testsuite/g++.dg/spellcheck-reswords.C @@ -0,0 +1,11 @@ +void pr81610 (void *p) +{ + forget (p); // { dg-error "not declared" } + // { dg-bogus "'for'" "" { target *-*-*} .-1 } +} + +void pr80567 (void *p) +{ + memset (p, 0, 4); // { dg-error "not declared" } + // { dg-bogus "'else'" "" { target *-*-*} .-1 } +} diff --git a/gcc/testsuite/g++.dg/spellcheck-stdlib.C b/gcc/testsuite/g++.dg/spellcheck-stdlib.C index 6e6ab1d..c7a6626 100644 --- a/gcc/testsuite/g++.dg/spellcheck-stdlib.C +++ b/gcc/testsuite/g++.dg/spellcheck-stdlib.C @@ -16,7 +16,7 @@ void test_cstdio (void) FILE *f; // { dg-error "'FILE' was not declared in this scope" } // { dg-message "'FILE' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } // { dg-error "'f' was not declared in this scope" "" { target *-*-* } .-2 } - // { dg-bogus "suggested alternative: 'if'" "PR c++/80567" { xfail *-*-* } .-3 } + // { dg-bogus "suggested alternative: 'if'" "PR c++/80567" { target *-*-* } .-3 } char buf[BUFSIZ]; // { dg-error "'BUFSIZ' was not declared" } // { dg-message "'BUFSIZ' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 }