This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Spelling suggestions for misspelled preprocessor directives


On Thu, Aug 18, 2016 at 11:28:02AM -0400, David Malcolm wrote:
> 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
> 
> I can self-approve all of it apart from the changes to c-family.
> 
> Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu.
> 
> OK for trunk?
> 
> 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.
> ---
>  gcc/c-family/c-common.c                           | 17 ++++++++++
>  gcc/c-family/c-common.h                           |  5 +++
>  gcc/c-family/c-lex.c                              |  1 +
>  gcc/testsuite/gcc.dg/cpp/misspelled-directive-1.c | 12 +++++++
>  gcc/testsuite/gcc.dg/cpp/misspelled-directive-2.c | 21 ++++++++++++
>  libcpp/directives.c                               | 41 +++++++++++++++++++++--
>  libcpp/errors.c                                   | 36 ++++++++++++++++++++
>  libcpp/include/cpplib.h                           |  8 +++++
>  8 files changed, 139 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/cpp/misspelled-directive-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/cpp/misspelled-directive-2.c
> 
> diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
> index 569f000..a8ef595 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.  */
>  
> @@ -12934,6 +12935,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)

Should be "const char *const" (i.e. no space).

> +{
> +  best_match<const char *, const char *> bm (goal);
> +  while (*candidates)
> +    bm.consider (*(candidates++));

I don't think you need the parens here.

> +  return bm.get_best_meaningful_candidate ();
> +}
> +
>  /* Check and possibly warn if two declarations have contradictory
>     attributes, such as always_inline vs. noinline.  */
>  
> diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
> index 61f9ced..a3da631 100644
> --- a/gcc/c-family/c-common.h
> +++ b/gcc/c-family/c-common.h
> @@ -1110,6 +1110,11 @@ extern time_t cb_get_source_date_epoch (cpp_reader *pfile);
>     __TIME__ can store.  */
>  #define MAX_SOURCE_DATE_EPOCH HOST_WIDE_INT_C (253402300799)
>  
> +/* Callback for libcpp for offering spelling suggestions for misspelled
> +   directives.  */
> +extern const char *cb_get_suggestion (cpp_reader *, const char *,
> +				      const char * const *);
> +

Same here.

Otherwise the c-family/ changes LGTM.

	Marek


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]