aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2016-08-18 18:52:43 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2016-08-18 18:52:43 +0000
commitcb18fd07f2962779c2651adc970541210d4ad98f (patch)
treeb325fd07e1c9c2f08551326602a989303e28ddfd /gcc
parenta76989dc7c9236214856db196da88a739f0e7baa (diff)
downloadgcc-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 'gcc')
-rw-r--r--gcc/c-family/ChangeLog8
-rw-r--r--gcc/c-family/c-common.c17
-rw-r--r--gcc/c-family/c-common.h5
-rw-r--r--gcc/c-family/c-lex.c1
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/gcc.dg/cpp/misspelled-directive-1.c12
-rw-r--r--gcc/testsuite/gcc.dg/cpp/misspelled-directive-2.c21
7 files changed, 69 insertions, 0 deletions
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index cc82370..1ed5268 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,11 @@
+2016-08-18 David Malcolm <dmalcolm@redhat.com>
+
+ * 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.
+
2016-08-18 Marek Polacek <polacek@redhat.com>
PR c/71514
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<const char *, const char *> 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. */
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 4673123..31320bf 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 *);
+
extern GTY(()) string_concat_db *g_string_concat_db;
/* libcpp can calculate location information about a range of characters
diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index 4c7e385..c904ee6 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -81,6 +81,7 @@ init_c_lex (void)
cb->read_pch = c_common_read_pch;
cb->has_attribute = c_common_has_attribute;
cb->get_source_date_epoch = cb_get_source_date_epoch;
+ cb->get_suggestion = cb_get_suggestion;
/* Set the debug callbacks if we can use them. */
if ((debug_info_level == DINFO_LEVEL_VERBOSE
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index e37bff4..21e9770 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2016-08-18 David Malcolm <dmalcolm@redhat.com>
+
+ * gcc.dg/cpp/misspelled-directive-1.c: New testcase.
+ * gcc.dg/cpp/misspelled-directive-2.c: New testcase.
+
2016-08-18 Marek Polacek <polacek@redhat.com>
PR c/71514
diff --git a/gcc/testsuite/gcc.dg/cpp/misspelled-directive-1.c b/gcc/testsuite/gcc.dg/cpp/misspelled-directive-1.c
new file mode 100644
index 0000000..f79670a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/misspelled-directive-1.c
@@ -0,0 +1,12 @@
+#ifndef SOME_GUARD /* { dg-error "unterminated" } */
+
+#if 1
+/* Typo here: "endfi" should have been "endif". */
+#endfi /* { dg-error "invalid preprocessing directive #endfi; did you mean #endif?" } */
+
+int make_non_empty;
+
+/* Another transposition typo: */
+#deifne FOO /* { dg-error "invalid preprocessing directive #deifne; did you mean #define?" } */
+
+#endif /* #ifndef SOME_GUARD */
diff --git a/gcc/testsuite/gcc.dg/cpp/misspelled-directive-2.c b/gcc/testsuite/gcc.dg/cpp/misspelled-directive-2.c
new file mode 100644
index 0000000..7ec5dee
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/misspelled-directive-2.c
@@ -0,0 +1,21 @@
+/* { dg-options "-fdiagnostics-show-caret" } */
+
+#endfi /* { dg-error "invalid preprocessing directive #endfi; did you mean #endif?" } */
+
+/* Verify that we offer fix-it hints. */
+/* { dg-begin-multiline-output "" }
+ #endfi
+ ^~~~~
+ endif
+ { dg-end-multiline-output "" } */
+
+/* Test coverage for the case of an unrecognized directive where no suggestion
+ is offered. */
+
+#this_does_not_match_anything /* { dg-error "invalid preprocessing directive #this_does_not_match_anything" } */
+/* { dg-begin-multiline-output "" }
+ #this_does_not_match_anything
+ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ { dg-end-multiline-output "" } */
+
+int make_non_empty;