From patchwork Wed Jul 20 14:46:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 650702 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 3rvfDl6P5Rz9t1S for ; Thu, 21 Jul 2016 00:19:35 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=i6K8bB5P; 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=HnpOVpQffm6qHsJRKr/GYF1iTxe3py6iUi89JPEI2+enR5m0hTMiO djEGn4xqtQIqugccUDvxnNFl7wrw9sIMQ8stNlUhyQsLE8pghNwuSoNqVoWmbEKV 27si1JWoKnXOjjUM8EI84fY5W5DJ1gruXEKCnUz5dnJPFThgkH53wo= 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=Q7qCi4whtFHTydVzlo0QwEaSVdQ=; b=i6K8bB5PNOEi3hNf6pYU Q2T4iYBAd3WFGAqMpn/DluQR6wa0C29r1MsNMNkKaKXNiajCijP5HdQvBLgshYoe KKqoor8qmSYAXeRzRXUyUgtGEJ1dFbEezBDFNYqXtv1zIcduTAjKOWz0ZfapNB6e 1FXqf0LmF/0B0ruDsV8VliQ= Received: (qmail 45946 invoked by alias); 20 Jul 2016 14:19:23 -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 45778 invoked by uid 89); 20 Jul 2016 14:19:22 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=misspelled, nonc, non-c, non-C 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; Wed, 20 Jul 2016 14:19:12 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (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 C83DA7F402 for ; Wed, 20 Jul 2016 14:19:10 +0000 (UTC) Received: from c64.redhat.com (vpn-227-177.phx2.redhat.com [10.3.227.177]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u6KEJ9WB013691; Wed, 20 Jul 2016 10:19:09 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: Jeff Law , Jason Merrill , David Malcolm Subject: [PATCH v2] C++ FE: handle misspelled identifiers and typenames Date: Wed, 20 Jul 2016 10:46:58 -0400 Message-Id: <1469026018-22172-1-git-send-email-dmalcolm@redhat.com> In-Reply-To: <6502df71-c1ed-8922-e054-a75cc0770fbf@redhat.com> References: <6502df71-c1ed-8922-e054-a75cc0770fbf@redhat.com> X-IsSubscribed: yes Changes in v2: - split out the non-C++ parts already approved by Jeff (I've committed these as r238522). - updated to mirror the fixes for PR c/71858 Jakub made to the corresponding C implementation in r238352, skipping anticipated decls of builtin functions - rewritten to more closely resemble the C FE's implementation This is a port of the C frontend's r237714 [1] to the C++ frontend: https://gcc.gnu.org/ml/gcc-patches/2016-06/msg01052.html offering spelling suggestions for misspelled identifiers, macro names, and some keywords (e.g. "singed" vs "signed" aka PR c/70339). Examples of suggestions can be seen in the test case. Successfully bootstrapped®rtested on x86_64-pc-linux-gnu; adds 267 PASS results to g++.sum. OK for trunk? [1] aka 8469aece13814deddf2cd80538d33c2d0a8d60d9 in the git mirror gcc/cp/ChangeLog: PR c/70339 PR c/71858 * name-lookup.c: Include gcc-rich-location.h, spellcheck-tree.h, and parser.h. (suggest_alternatives_for): If no candidates are found, try lookup_name_fuzzy and report if if finds a suggestion. (consider_binding_level): New function. (lookup_name_fuzzy) New function. * parser.c: Include gcc-rich-location.h. (cp_lexer_next_token_is_decl_specifier_keyword): Move most of logic into... (cp_keyword_starts_decl_specifier_p): ...this new function. (cp_parser_diagnose_invalid_type_name): When issuing "does not name a type" errors, attempt to make a suggestion using lookup_name_fuzzy. * parser.h (cp_keyword_starts_decl_specifier_p): New prototype. * search.c (lookup_field_fuzzy_info::fuzzy_lookup_field): Don't attempt to access TYPE_FIELDS within a TYPE_PACK_EXPANSION. gcc/testsuite/ChangeLog: PR c/70339 PR c/71858 * g++.dg/spellcheck-identifiers.C: New test case, based on gcc.dg/spellcheck-identifiers.c. * g++.dg/spellcheck-identifiers-2.C: New test case, based on gcc.dg/spellcheck-identifiers-2.c. * g++.dg/spellcheck-typenames.C: New test case, based on gcc.dg/spellcheck-typenames.c --- gcc/cp/name-lookup.c | 116 ++++++++++- gcc/cp/parser.c | 43 +++- gcc/cp/parser.h | 1 + gcc/cp/search.c | 4 + gcc/testsuite/g++.dg/spellcheck-identifiers-2.C | 43 ++++ gcc/testsuite/g++.dg/spellcheck-identifiers.C | 255 ++++++++++++++++++++++++ gcc/testsuite/g++.dg/spellcheck-typenames.C | 84 ++++++++ 7 files changed, 533 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/g++.dg/spellcheck-identifiers-2.C create mode 100644 gcc/testsuite/g++.dg/spellcheck-identifiers.C create mode 100644 gcc/testsuite/g++.dg/spellcheck-typenames.C diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index cbd5209..561bf71 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -29,6 +29,9 @@ along with GCC; see the file COPYING3. If not see #include "debug.h" #include "c-family/c-pragma.h" #include "params.h" +#include "gcc-rich-location.h" +#include "spellcheck-tree.h" +#include "parser.h" /* The bindings for a particular name in a particular scope. */ @@ -4435,9 +4438,20 @@ suggest_alternatives_for (location_t location, tree name) namespaces_to_search.release (); - /* Nothing useful to report. */ + /* Nothing useful to report for NAME. Report on likely misspellings, + or do nothing. */ if (candidates.is_empty ()) - return; + { + const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME); + if (fuzzy_name) + { + gcc_rich_location richloc (location); + richloc.add_fixit_misspelled_id (location, fuzzy_name); + inform_at_rich_loc (&richloc, "suggested alternative: %qs", + fuzzy_name); + } + return; + } inform_n (location, candidates.length (), "suggested alternative:", @@ -4672,6 +4686,104 @@ qualified_lookup_using_namespace (tree name, tree scope, return result->value != error_mark_node; } +/* Helper function for lookup_name_fuzzy. + Traverse binding level LVL, looking for good name matches for NAME + (and BM). */ +static void +consider_binding_level (tree name, best_match &bm, + cp_binding_level *lvl, bool look_within_fields, + enum lookup_name_fuzzy_kind kind) +{ + if (look_within_fields) + if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE) + { + tree type = lvl->this_entity; + bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME); + tree best_matching_field + = lookup_member_fuzzy (type, name, want_type_p); + if (best_matching_field) + bm.consider (best_matching_field); + } + + for (tree t = lvl->names; t; t = TREE_CHAIN (t)) + { + /* Don't use bindings from implicitly declared functions, + as they were likely misspellings themselves. */ + if (TREE_TYPE (t) == error_mark_node) + continue; + + /* Skip anticipated decls of builtin functions. */ + if (TREE_CODE (t) == FUNCTION_DECL) + if (DECL_BUILT_IN (t)) + if (DECL_ANTICIPATED (t)) + continue; + + if (DECL_NAME (t)) + bm.consider (DECL_NAME (t)); + } +} + +/* 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 + no reasonable match is found. */ + +const char * +lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind) +{ + gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); + + best_match bm (name); + + cp_binding_level *lvl; + for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain) + consider_binding_level (name, bm, lvl, true, kind); + + for (lvl = current_binding_level; lvl; lvl = lvl->level_chain) + consider_binding_level (name, bm, lvl, false, kind); + + /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO" + as: + x = SOME_OTHER_MACRO (y); + then "SOME_OTHER_MACRO" will survive to the frontend and show up + as a misspelled identifier. + + Use the best distance so far so that a candidate is only set if + a macro is better than anything so far. This allows early rejection + (without calculating the edit distance) of macro names that must have + distance >= bm.get_best_distance (), and means that we only get a + non-NULL result for best_macro_match if it's better than any of + the identifiers already checked. */ + best_macro_match bmm (name, bm.get_best_distance (), parse_in); + cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate (); + /* If a macro is the closest so far to NAME, suggest it. */ + if (best_macro) + return (const char *)best_macro->ident.str; + + /* Try the "starts_decl_specifier_p" keywords to detect + "singed" vs "signed" typos. */ + for (unsigned i = 0; i < num_c_common_reswords; i++) + { + const c_common_resword *resword = &c_common_reswords[i]; + + if (!cp_keyword_starts_decl_specifier_p (resword->rid)) + continue; + + tree resword_identifier = ridpointers [resword->rid]; + if (!resword_identifier) + continue; + gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE); + bm.consider (resword_identifier); + } + + /* See if we have a good suggesion for the user. */ + tree best_id = bm.get_best_meaningful_candidate (); + if (best_id) + return IDENTIFIER_POINTER (best_id); + + /* No meaningful suggestion available. */ + return NULL; +} + /* Subroutine of outer_binding. Returns TRUE if BINDING is a binding to a template parameter of diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 0a0f67b..7df75d6 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -40,6 +40,7 @@ along with GCC; see the file COPYING3. If not see #include "c-family/c-indentation.h" #include "context.h" #include "cp-cilkplus.h" +#include "gcc-rich-location.h" /* The lexer. */ @@ -937,15 +938,12 @@ cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword) return cp_lexer_peek_token (lexer)->keyword != keyword; } -/* Return true if the next token is a keyword for a decl-specifier. */ +/* Return true if KEYWORD can start a decl-specifier. */ -static bool -cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer) +bool +cp_keyword_starts_decl_specifier_p (enum rid keyword) { - cp_token *token; - - token = cp_lexer_peek_token (lexer); - switch (token->keyword) + switch (keyword) { /* auto specifier: storage-class-specifier in C++, simple-type-specifier in C++0x. */ @@ -985,14 +983,25 @@ cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer) return true; default: - if (token->keyword >= RID_FIRST_INT_N - && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS - && int_n_enabled_p[token->keyword - RID_FIRST_INT_N]) + if (keyword >= RID_FIRST_INT_N + && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS + && int_n_enabled_p[keyword - RID_FIRST_INT_N]) return true; return false; } } +/* Return true if the next token is a keyword for a decl-specifier. */ + +static bool +cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer) +{ + cp_token *token; + + token = cp_lexer_peek_token (lexer); + return cp_keyword_starts_decl_specifier_p (token->keyword); +} + /* Returns TRUE iff the token T begins a decltype type. */ static bool @@ -3154,7 +3163,19 @@ cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id, else if (!parser->scope) { /* Issue an error message. */ - error_at (location, "%qE does not name a type", id); + const char *suggestion = NULL; + if (TREE_CODE (id) == IDENTIFIER_NODE) + suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME); + if (suggestion) + { + gcc_rich_location richloc (location); + richloc.add_fixit_misspelled_id (location, suggestion); + error_at_rich_loc (&richloc, + "%qE does not name a type; did you mean %qs?", + id, suggestion); + } + else + error_at (location, "%qE does not name a type", id); /* If we're in a template class, it's possible that the user was referring to a type from a base class. For example: diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h index ccbace9..2923378 100644 --- a/gcc/cp/parser.h +++ b/gcc/cp/parser.h @@ -420,5 +420,6 @@ extern void debug (vec *ptr); extern void cp_debug_parser (FILE *, cp_parser *); extern void debug (cp_parser &ref); extern void debug (cp_parser *ptr); +extern bool cp_keyword_starts_decl_specifier_p (enum rid keyword); #endif /* GCC_CP_PARSER_H */ diff --git a/gcc/cp/search.c b/gcc/cp/search.c index 990c3fe..4a862b7 100644 --- a/gcc/cp/search.c +++ b/gcc/cp/search.c @@ -1407,6 +1407,10 @@ lookup_field_fuzzy_info::fuzzy_lookup_field (tree type) The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */ return; + /* TYPE_FIELDS is not valid for a TYPE_PACK_EXPANSION. */ + if (TREE_CODE (type) == TYPE_PACK_EXPANSION) + return; + for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) { if (!m_want_type_p || DECL_DECLARES_TYPE_P (field)) diff --git a/gcc/testsuite/g++.dg/spellcheck-identifiers-2.C b/gcc/testsuite/g++.dg/spellcheck-identifiers-2.C new file mode 100644 index 0000000..59a8ec5 --- /dev/null +++ b/gcc/testsuite/g++.dg/spellcheck-identifiers-2.C @@ -0,0 +1,43 @@ +/* PR c/71858 */ +/* Make sure anticipated builtins are not considered before they are declared. */ +/* { dg-do compile } */ +/* { dg-options "-fdiagnostics-show-caret" } */ + +int sscafn (const char *, const char *, ...); + +int +test_1 (const char *p) +{ + int i; + return ssacnf (p, "%d", &i); /* { dg-error "10: .ssacnf. was not declared in this scope" } */ + /* { dg-begin-multiline-output "" } + return ssacnf (p, "%d", &i); + ^~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'sscafn'" "" { target *-*-* } 12 } + /* { dg-begin-multiline-output "" } + return ssacnf (p, "%d", &i); + ^~~~~~ + sscafn + { dg-end-multiline-output "" } */ +} + +int scafn (const char *, ...); +int scanf (const char *, ...); + +int +test_2 (void) +{ + int i; + return sacnf ("%d", &i); /* { dg-error "10: .sacnf. was not declared in this scope" } */ + /* { dg-begin-multiline-output "" } + return sacnf ("%d", &i); + ^~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'scanf'" "" { target *-*-* } 32 } + /* { dg-begin-multiline-output "" } + return sacnf ("%d", &i); + ^~~~~ + scanf + { dg-end-multiline-output "" } */ +} diff --git a/gcc/testsuite/g++.dg/spellcheck-identifiers.C b/gcc/testsuite/g++.dg/spellcheck-identifiers.C new file mode 100644 index 0000000..0843439 --- /dev/null +++ b/gcc/testsuite/g++.dg/spellcheck-identifiers.C @@ -0,0 +1,255 @@ +/* { dg-do compile } */ +/* { dg-options "-fdiagnostics-show-caret" } */ + +typedef struct GtkWidget { int dummy; } GtkWidget; + +extern void gtk_widget_show_all (GtkWidget *w); + + +void +test_1 (GtkWidget *w) +{ + gtk_widget_showall (w); // { dg-error "3: 'gtk_widget_showall' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + gtk_widget_showall (w); + ^~~~~~~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "3: suggested alternative: 'gtk_widget_show_all'" "" { target *-*-* } 12 } + /* { dg-begin-multiline-output "" } + gtk_widget_showall (w); + ^~~~~~~~~~~~~~~~~~ + gtk_widget_show_all + { dg-end-multiline-output "" } */ + + /* Ensure we don't try to suggest "gtk_widget_showall" for subsequent + corrections. */ + gtk_widget_showall_ (w); // { dg-error "3: 'gtk_widget_showall_' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + gtk_widget_showall_ (w); + ^~~~~~~~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "3: suggested alternative: 'gtk_widget_show_all'" "" { target *-*-* } 26 } + /* { dg-begin-multiline-output "" } + gtk_widget_showall_ (w); + ^~~~~~~~~~~~~~~~~~~ + gtk_widget_show_all + { dg-end-multiline-output "" } */ + + GtkWidgetShowAll (w); // { dg-error "3: 'GtkWidgetShowAll' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + GtkWidgetShowAll (w); + ^~~~~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "3: suggested alternative: 'gtk_widget_show_all'" "" { target *-*-* } 38 } + /* { dg-begin-multiline-output "" } + GtkWidgetShowAll (w); + ^~~~~~~~~~~~~~~~ + gtk_widget_show_all + { dg-end-multiline-output "" } */ +} + +int +test_2 (int param) +{ + return parma * parma; // { dg-error "10: 'parma' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + return parma * parma; + ^~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'param'" "" { target *-*-* } 54 } + /* { dg-begin-multiline-output "" } + return parma * parma; + ^~~~~ + param + { dg-end-multiline-output "" } */ +} + +#define MACRO(X) ((X)) + +int +test_3 (int i) +{ + return MACRAME (i); // { dg-error "10: 'MACRAME' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + return MACRAME (i); + ^~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'MACRO'" "" { target *-*-* } 72 } + /* { dg-begin-multiline-output "" } + return MACRAME (i); + ^~~~~~~ + MACRO + { dg-end-multiline-output "" } */ +} + +#define IDENTIFIER_POINTER(X) ((X)) + +int +test_4 (int node) +{ + return IDENTIFIER_PTR (node); // { dg-error "10: 'IDENTIFIER_PTR' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + return IDENTIFIER_PTR (node); + ^~~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'IDENTIFIER_POINTER'" "" { target *-*-* } 90 } + /* { dg-begin-multiline-output "" } + return IDENTIFIER_PTR (node); + ^~~~~~~~~~~~~~ + IDENTIFIER_POINTER + { dg-end-multiline-output "" } */ +} + + +int +test_5 (void) +{ + return __LINE_; /* { dg-error "10: '__LINE_' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + return __LINE_; + ^~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: '__LINE__'" "" { target *-*-* } 107 } + /* { dg-begin-multiline-output "" } + return __LINE_; + ^~~~~~~ + __LINE__ + { dg-end-multiline-output "" } */ +} + +#define MAX_ITEMS 100 +int array[MAX_ITEM]; // { dg-error "11: 'MAX_ITEM' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + int array[MAX_ITEM]; + ^~~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "11: suggested alternative: 'MAX_ITEMS'" "" { target *-*-* } 121 } + /* { dg-begin-multiline-output "" } + int array[MAX_ITEM]; + ^~~~~~~~ + MAX_ITEMS + { dg-end-multiline-output "" } */ + + +enum foo { + FOO_FIRST, + FOO_SECOND +}; + +int +test_6 (enum foo f) +{ + switch (f) + { + case FOO_FURST: // { dg-error "10: 'FOO_FURST' was not declared in this scope" } + break; + /* { dg-begin-multiline-output "" } + case FOO_FURST: + ^~~~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'FOO_FIRST'" "" { target *-*-* } 144 } + /* { dg-begin-multiline-output "" } + case FOO_FURST: + ^~~~~~~~~ + FOO_FIRST + { dg-end-multiline-output "" } */ + + case FOO_SECCOND: // { dg-error "10: 'FOO_SECCOND' was not declared in this scope" } + break; + /* { dg-begin-multiline-output "" } + case FOO_SECCOND: + ^~~~~~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'FOO_SECOND'" "" { target *-*-* } 157 } + /* { dg-begin-multiline-output "" } + case FOO_SECCOND: + ^~~~~~~~~~~ + FOO_SECOND + { dg-end-multiline-output "" } */ + + default: + break; + } +} + +int snprintf (char *, __SIZE_TYPE__, const char *, ...); + +void +test_7 (int i, int j) +{ + int buffer[100]; + snprint (buffer, 100, "%i of %i", i, j); // { dg-error "3: 'snprint' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + snprint (buffer, 100, "%i of %i", i, j); + ^~~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "3: suggested alternative: 'snprintf'" "" { target *-*-* } 181 } + /* { dg-begin-multiline-output "" } + snprint (buffer, 100, "%i of %i", i, j); + ^~~~~~~ + snprintf + { dg-end-multiline-output "" } */ +} + +int +test_8 () +{ + int local = 42; + + return locale; // { dg-error "10: 'locale' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + return locale; + ^~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'local'" "" { target *-*-* } 199 } + /* { dg-begin-multiline-output "" } + return locale; + ^~~~~~ + local + { dg-end-multiline-output "" } */ +} + +class base +{ +public: + int test_method_1 (); + +protected: + int m_foo; +}; + +class sub : public base +{ +public: + int test_method_2 (); +}; + +int base::test_method_1 () +{ + return m_food; // { dg-error "10: 'm_food' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + return m_food; + ^~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'm_foo'" "" { target *-*-* } 229 } + /* { dg-begin-multiline-output "" } + return m_food; + ^~~~~~ + m_foo + { dg-end-multiline-output "" } */ +} + +int sub::test_method_2 () +{ + return m_food; // { dg-error "10: 'm_food' was not declared in this scope" } + /* { dg-begin-multiline-output "" } + return m_food; + ^~~~~~ + { dg-end-multiline-output "" } */ + // { dg-message "10: suggested alternative: 'm_foo'" "" { target *-*-* } 244 } + /* { dg-begin-multiline-output "" } + return m_food; + ^~~~~~ + m_foo + { dg-end-multiline-output "" } */ +} diff --git a/gcc/testsuite/g++.dg/spellcheck-typenames.C b/gcc/testsuite/g++.dg/spellcheck-typenames.C new file mode 100644 index 0000000..9aa5b72 --- /dev/null +++ b/gcc/testsuite/g++.dg/spellcheck-typenames.C @@ -0,0 +1,84 @@ +/* { dg-do compile } */ +/* { dg-options "-fdiagnostics-show-caret" } */ + +void test_1 (signed char e); + +/* PR c/70339. */ +void test_2 (singed char e); // { dg-error "21: variable or field 'test_2' declared void" } +/* { dg-begin-multiline-output "" } + void test_2 (singed char e); + ^~~~ + { dg-end-multiline-output "" } */ +// { dg-message "14: 'singed' was not declared in this scope" "" { target *-*-* } 7 } +/* { dg-begin-multiline-output "" } + void test_2 (singed char e); + ^~~~~~ + { dg-end-multiline-output "" } */ +// { dg-message "14: suggested alternative: 'signed'" "" { target *-*-* } 7 } +/* { dg-begin-multiline-output "" } + void test_2 (singed char e); + ^~~~~~ + signed + { dg-end-multiline-output "" } */ + +void test_3 (car e); // { dg-error "14: variable or field 'test_3' declared void" } +/* { dg-begin-multiline-output "" } + void test_3 (car e); + ^~~ + { dg-end-multiline-output "" } */ +// { dg-message "14: 'car' was not declared in this scope" "" { target *-*-* } 24 } +// { dg-message "14: suggested alternative: 'char'" "" { target *-*-* } 24 } +/* { dg-begin-multiline-output "" } + void test_3 (car e); + ^~~ + char + { dg-end-multiline-output "" } */ + +/* TODO: this one could be handled better. */ +void test_4 (signed car e); // { dg-error "25: expected ',' or '...' before 'e'" } +/* { dg-begin-multiline-output "" } + void test_4 (signed car e); + ^ + { dg-end-multiline-output "" } */ + +/* Verify that we handle misspelled typedef names. */ + +typedef struct something {} something_t; + +some_thing_t test_5; // { dg-error "1: 'some_thing_t' does not name a type; did you mean 'something_t'?" } + /* { dg-begin-multiline-output "" } + some_thing_t test_5; + ^~~~~~~~~~~~ + something_t + { dg-end-multiline-output "" } */ + +/* TODO: we don't yet handle misspelled struct names. */ +struct some_thing test_6; // { dg-error "aggregate 'some_thing test_6' has incomplete type and cannot be defined" } + /* { dg-begin-multiline-output "" } + struct some_thing test_6; + ^~~~~~ + { dg-end-multiline-output "" } */ + +typedef long int64_t; +int64 i; // { dg-error "1: 'int64' does not name a type; did you mean 'int64_t'?" } +/* { dg-begin-multiline-output "" } + int64 i; + ^~~~~ + int64_t + { dg-end-multiline-output "" } */ + +/* Verify that gcc doesn't offer nonsensical suggestions. */ + +nonsensical_suggestion_t var; /* { dg-bogus "did you mean" } */ +/* { dg-error "'nonsensical_suggestion_t' does not name a type" "" { target { *-*-* } } 72 } */ +/* { dg-begin-multiline-output "" } + nonsensical_suggestion_t var; + ^~~~~~~~~~~~~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ + +singed char ch; // { dg-error "1: 'singed' does not name a type; did you mean 'signed'?" } +/* { dg-begin-multiline-output "" } + singed char ch; + ^~~~~~ + signed + { dg-end-multiline-output "" } */