diff options
Diffstat (limited to 'libcpp')
-rw-r--r-- | libcpp/ChangeLog | 11 | ||||
-rw-r--r-- | libcpp/directives.c | 41 | ||||
-rw-r--r-- | libcpp/errors.c | 36 | ||||
-rw-r--r-- | libcpp/include/cpplib.h | 8 |
4 files changed, 94 insertions, 2 deletions
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index e700dfe..5d4a094 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,3 +1,14 @@ +2016-08-18 David Malcolm <dmalcolm@redhat.com> + + * 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. + 2016-08-18 Marek Polacek <polacek@redhat.com> PR c/7652 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; diff --git a/libcpp/errors.c b/libcpp/errors.c index f7d4112..3b0a0b4 100644 --- a/libcpp/errors.c +++ b/libcpp/errors.c @@ -31,6 +31,23 @@ along with this program; see the file COPYING3. If not see ATTRIBUTE_FPTR_PRINTF(5,0) static bool +cpp_diagnostic_at_richloc (cpp_reader * pfile, int level, int reason, + rich_location *richloc, + const char *msgid, va_list *ap) +{ + bool ret; + + if (!pfile->cb.error) + abort (); + ret = pfile->cb.error (pfile, level, reason, richloc, _(msgid), ap); + + return ret; +} + +/* Print a diagnostic at the given location. */ + +ATTRIBUTE_FPTR_PRINTF(5,0) +static bool cpp_diagnostic_at (cpp_reader * pfile, int level, int reason, source_location src_loc, const char *msgid, va_list *ap) @@ -255,6 +272,25 @@ cpp_error_at (cpp_reader * pfile, int level, source_location src_loc, return ret; } +/* As cpp_error, but use RICHLOC as the location of the error, without + a column override. */ + +bool +cpp_error_at_richloc (cpp_reader * pfile, int level, rich_location *richloc, + const char *msgid, ...) +{ + va_list ap; + bool ret; + + va_start (ap, msgid); + + ret = cpp_diagnostic_at_richloc (pfile, level, CPP_W_NONE, richloc, + msgid, &ap); + + va_end (ap); + return ret; +} + /* Print a warning or error, depending on the value of LEVEL. Include information from errno. */ diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 659686b..a497811 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -597,6 +597,9 @@ struct cpp_callbacks /* Callback to parse SOURCE_DATE_EPOCH from environment. */ time_t (*get_source_date_epoch) (cpp_reader *); + + /* Callback for providing suggestions for misspelled directives. */ + const char *(*get_suggestion) (cpp_reader *, const char *, const char *const *); }; #ifdef VMS @@ -1066,6 +1069,11 @@ extern bool cpp_error_at (cpp_reader * pfile, int level, source_location src_loc, const char *msgid, ...) ATTRIBUTE_PRINTF_4; +extern bool cpp_error_at_richloc (cpp_reader * pfile, int level, + rich_location *richloc, const char *msgid, + ...) + ATTRIBUTE_PRINTF_4; + /* In lex.c */ extern int cpp_ideq (const cpp_token *, const char *); extern void cpp_output_line (cpp_reader *, FILE *); |