diff options
author | David Malcolm <dmalcolm@redhat.com> | 2016-08-18 18:52:43 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2016-08-18 18:52:43 +0000 |
commit | cb18fd07f2962779c2651adc970541210d4ad98f (patch) | |
tree | b325fd07e1c9c2f08551326602a989303e28ddfd /libcpp/directives.c | |
parent | a76989dc7c9236214856db196da88a739f0e7baa (diff) | |
download | gcc-cb18fd07f2962779c2651adc970541210d4ad98f.zip gcc-cb18fd07f2962779c2651adc970541210d4ad98f.tar.gz gcc-cb18fd07f2962779c2651adc970541210d4ad98f.tar.bz2 |
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
Diffstat (limited to 'libcpp/directives.c')
-rw-r--r-- | libcpp/directives.c | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/libcpp/directives.c b/libcpp/directives.c index 772b835..c0006a4 100644 --- a/libcpp/directives.c +++ b/libcpp/directives.c @@ -188,6 +188,16 @@ static const directive dtable[] = DIRECTIVE_TABLE }; #undef D + +/* A NULL-terminated array of directive names for use + when suggesting corrections for misspelled directives. */ +#define D(name, t, origin, flags) #name, +static const char * const directive_names[] = { +DIRECTIVE_TABLE + NULL +}; +#undef D + #undef DIRECTIVE_TABLE /* Wrapper struct directive for linemarkers. @@ -498,8 +508,35 @@ _cpp_handle_directive (cpp_reader *pfile, int indented) if (CPP_OPTION (pfile, lang) == CLK_ASM) skip = 0; else if (!pfile->state.skipping) - cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", - cpp_token_as_text (pfile, dname)); + { + const char *unrecognized + = (const char *)cpp_token_as_text (pfile, dname); + const char *hint = NULL; + + /* Call back into gcc to get a spelling suggestion. Ideally + we'd just use best_match from gcc/spellcheck.h (and filter + out the uncommon directives), but that requires moving it + to a support library. */ + if (pfile->cb.get_suggestion) + hint = pfile->cb.get_suggestion (pfile, unrecognized, + directive_names); + + if (hint) + { + rich_location richloc (pfile->line_table, dname->src_loc); + source_range misspelled_token_range + = get_range_from_loc (pfile->line_table, dname->src_loc); + richloc.add_fixit_replace (misspelled_token_range, hint); + cpp_error_at_richloc (pfile, CPP_DL_ERROR, &richloc, + "invalid preprocessing directive #%s;" + " did you mean #%s?", + unrecognized, hint); + } + else + cpp_error (pfile, CPP_DL_ERROR, + "invalid preprocessing directive #%s", + unrecognized); + } } pfile->directive = dir; |