From cb18fd07f2962779c2651adc970541210d4ad98f Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 18 Aug 2016 18:52:43 +0000 Subject: Spelling suggestions for misspelled preprocessor directives This patch allows the preprocessor to offer suggestions for misspelled directives, taking us from e.g.: test.c:5:2: error: invalid preprocessing directive #endfi #endfi ^~~~~ to: test.c:5:2: error: invalid preprocessing directive #endfi; did you mean #endif? #endfi ^~~~~ endif gcc/c-family/ChangeLog: * c-common.c: Include "spellcheck.h". (cb_get_suggestion): New function. * c-common.h (cb_get_suggestion): New decl. * c-lex.c (init_c_lex): Initialize cb->get_suggestion to cb_get_suggestion. gcc/testsuite/ChangeLog: * gcc.dg/cpp/misspelled-directive-1.c: New testcase. * gcc.dg/cpp/misspelled-directive-2.c: New testcase. libcpp/ChangeLog: * directives.c (directive_names): New array. (_cpp_handle_directive): Offer spelling suggestions for misspelled directives. * errors.c (cpp_diagnostic_at_richloc): New function. (cpp_error_at_richloc): New function. * include/cpplib.h (struct cpp_callbacks): Add field "get_suggestion". (cpp_error_at_richloc): New decl. From-SVN: r239585 --- gcc/c-family/c-common.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'gcc/c-family/c-common.c') diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 22e3844..9082883 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -46,6 +46,7 @@ along with GCC; see the file COPYING3. If not see #include "opts.h" #include "gimplify.h" #include "substring-locations.h" +#include "spellcheck.h" cpp_reader *parse_in; /* Declared in c-pragma.h. */ @@ -12948,6 +12949,22 @@ cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED) return (time_t) epoch; } +/* Callback for libcpp for offering spelling suggestions for misspelled + directives. GOAL is an unrecognized string; CANDIDATES is a + NULL-terminated array of candidate strings. Return the closest + match to GOAL within CANDIDATES, or NULL if none are good + suggestions. */ + +const char * +cb_get_suggestion (cpp_reader *, const char *goal, + const char *const *candidates) +{ + best_match bm (goal); + while (*candidates) + bm.consider (*candidates++); + return bm.get_best_meaningful_candidate (); +} + /* Check and possibly warn if two declarations have contradictory attributes, such as always_inline vs. noinline. */ -- cgit v1.1