From patchwork Thu Apr 28 14:28:17 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 616278 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3qwdqV5RBLz9sBc for ; Fri, 29 Apr 2016 00:04:22 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=SvKFhDue; dkim-atps=neutral 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:in-reply-to:references; q=dns; s= default; b=ZDoV0qPaLRGRoDeCyTZFPc0j3zdc/1mAWoHR+2ilX0vY4r3fA1+t2 F6iq8uytLebm/t1ejCoLrXJ4pW6UhXNMZD5rOzGqmAoV3Gi/z8euTVbmfsaGnz0s STCs/2VWjI5+pzpgDnBVH7j8CKeHtGzxJQwdFSnHZYrELEnr9rFW7M= 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:in-reply-to:references; s= default; bh=pe+29zmC2g08b46vc1WXOEBuZ1M=; b=SvKFhDueR3udcbuTzncb 10lNoNxglnDPRYbVo4C5pq930SBR5Vabgc+gI4KK6KcPW5Zqf/e7G5B/fnsiA4oZ h6NbTYA8jE9S06mPRtC6/vAbl4Gr8PLsfEYD6F/UCXipCwBjNMPQjZjgcf2nJNYq Edwvye101ZX/D1r8rX9K9/g= Received: (qmail 25079 invoked by alias); 28 Apr 2016 14:03:49 -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 25030 invoked by uid 89); 28 Apr 2016 14:03:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.9 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=aid 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 (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 28 Apr 2016 14:03:37 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 242DC811DF for ; Thu, 28 Apr 2016 14:03:36 +0000 (UTC) Received: from c64.redhat.com (vpn-235-58.phx2.redhat.com [10.3.235.58]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3SE3YrN002851; Thu, 28 Apr 2016 10:03:35 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 3/4] PR c++/62314: C++: add fixit hint to misspelled member names Date: Thu, 28 Apr 2016 10:28:17 -0400 Message-Id: <1461853698-43954-3-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1461853698-43954-1-git-send-email-dmalcolm@redhat.com> References: <1461853698-43954-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes When we emit a hint about a misspelled member name, it will slightly aid readability if we use a fixit-hint to show the proposed name in context within the source code (and in the future this might support some kind of auto-apply in an IDE). This patch adds such a hint to the C++ frontend, taking us from: test.cc:10:15: error: 'struct foo' has no member named 'colour'; did you mean 'color'? return ptr->colour; ^~~~~~ to: test.cc:10:15: error: 'struct foo' has no member named 'colour'; did you mean 'color'? return ptr->colour; ^~~~~~ color Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. OK for trunk? gcc/cp/ChangeLog: PR c++/62314 * typeck.c (finish_class_member_access_expr): When giving a hint about a possibly-misspelled member name, add a fix-it replacement hint. gcc/testsuite/ChangeLog: PR c++/62314 * g++.dg/spellcheck-fields-2.C: New test case. --- gcc/cp/typeck.c | 18 +++++++++++++++--- gcc/testsuite/g++.dg/spellcheck-fields-2.C | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/spellcheck-fields-2.C diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 7e12009..95c777d 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -2817,9 +2817,21 @@ finish_class_member_access_expr (cp_expr object, tree name, bool template_p, tree guessed_id = lookup_member_fuzzy (access_path, name, /*want_type=*/false); if (guessed_id) - error ("%q#T has no member named %qE; did you mean %qE?", - TREE_CODE (access_path) == TREE_BINFO - ? TREE_TYPE (access_path) : object_type, name, guessed_id); + { + location_t bogus_component_loc = input_location; + rich_location rich_loc (line_table, bogus_component_loc); + source_range bogus_component_range = + get_range_from_loc (line_table, bogus_component_loc); + rich_loc.add_fixit_replace + (bogus_component_range, + IDENTIFIER_POINTER (guessed_id)); + error_at_rich_loc + (&rich_loc, + "%q#T has no member named %qE; did you mean %qE?", + TREE_CODE (access_path) == TREE_BINFO + ? TREE_TYPE (access_path) : object_type, name, + guessed_id); + } else error ("%q#T has no member named %qE", TREE_CODE (access_path) == TREE_BINFO diff --git a/gcc/testsuite/g++.dg/spellcheck-fields-2.C b/gcc/testsuite/g++.dg/spellcheck-fields-2.C new file mode 100644 index 0000000..eb10b44 --- /dev/null +++ b/gcc/testsuite/g++.dg/spellcheck-fields-2.C @@ -0,0 +1,19 @@ +// { dg-options "-fdiagnostics-show-caret" } + +union u +{ + int color; + int shape; +}; + +int test (union u *ptr) +{ + return ptr->colour; // { dg-error "did you mean .color.?" } +} + +// Verify that we get an underline and a fixit hint. +/* { dg-begin-multiline-output "" } + return ptr->colour; + ^~~~~~ + color + { dg-end-multiline-output "" } */