From d5678a83260d8853d86441e802597be16189eaa5 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 12 Jun 2017 16:38:40 -0400
Subject: [PATCH 21/44] FIXME: rework spellchecking to work on tokens, rather
 than on callback (to respect #if 0)

---
 gcc/c-family/c-lex.c     | 19 +++++++++++++------
 gcc/c-family/c-opts.c    |  5 ++++-
 gcc/spellcheck-enchant.c | 12 ++++++++----
 gcc/spellcheck-enchant.h |  7 +++----
 4 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index ed2617c..26cfe29 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -85,9 +85,7 @@ init_c_lex (void)
   cb->get_suggestion = cb_get_suggestion;
   if (warn_spellcheck_comments)
     {
-#ifdef HAVE_ENCHANT
-      cb->comment = spellcheck_enchant_check_comment;
-#else
+#ifndef HAVE_ENCHANT
       sorry ("this build of gcc was not configured with libenchant");
 #endif
     }
@@ -605,10 +603,19 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
     case CPP_MACRO_ARG:
       gcc_unreachable ();
 
-    /* CPP_COMMENT will appear when compiling with -C.  Ignore, except
-       when it is a FALLTHROUGH comment, in that case set
-       PREV_FALLTHROUGH flag on the next non-comment token.  */
+    /* CPP_COMMENT will appear when compiling with -C, or when
+       -Wspellcheck-comments is enabled.  */
     case CPP_COMMENT:
+      /* Potentially spellcheck the token.  */
+      if (warn_spellcheck_comments)
+	{
+#ifdef HAVE_ENCHANT
+	  spellcheck_enchant_check_comment (tok, *loc);
+#endif /* #ifdef HAVE_ENCHANT  */
+	}
+
+      /* If it is a FALLTHROUGH comment, set PREV_FALLTHROUGH flag on the
+	 next non-comment token.  */
       if (tok->flags & PREV_FALLTHROUGH)
 	{
 	  do
diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 7ebfe01..41bc075 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -996,7 +996,10 @@ c_common_post_options (const char **pfilename)
     {
 #ifdef HAVE_ENCHANT
       if (warn_spellcheck_comments)
-	spellcheck_enchant_init (warn_spellcheck_comments);
+	{
+	  spellcheck_enchant_init (warn_spellcheck_comments);
+	  cpp_opts->discard_comments = false;
+	}
 #endif
       init_c_lex ();
 
diff --git a/gcc/spellcheck-enchant.c b/gcc/spellcheck-enchant.c
index 04042db..73a8db8 100644
--- a/gcc/spellcheck-enchant.c
+++ b/gcc/spellcheck-enchant.c
@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "diagnostic.h"
 #include "options.h"
 #include "stringpool.h"
+#include "cpplib.h"
 #include "selftest.h"
 
 /* This file is guarded by "#ifdef HAVE_ENCHANT".  */
@@ -310,17 +311,20 @@ spellcheck_enchant_init (const char *language_tag)
     ("/home/david/coding-3/gcc-git-fixits/src/gcc/gcc-wordlist.txt");
 }
 
-/* Callback for cpp_callbacks::comments for spellchecking comments
-   using Enchant.  */
+/* Spellcheck comments within TOKEN using Enchant.  */
 
 void
-spellcheck_enchant_check_comment (cpp_reader *, source_location loc,
-				  const unsigned char *content, size_t len)
+spellcheck_enchant_check_comment (const cpp_token *token, location_t loc)
 {
   /* Don't spellcheck within system headers.  */
   if (in_system_header_at (loc))
     return;
 
+  gcc_assert (token);
+  gcc_assert (token->type ==  CPP_COMMENT);
+  const unsigned char *content = token->val.str.text;
+  size_t len = token->val.str.len;
+
   /* Split into words, tracking the location, and call spellcheck_word
      on each word.  */
 
diff --git a/gcc/spellcheck-enchant.h b/gcc/spellcheck-enchant.h
index ac069f7..0e6eb86 100644
--- a/gcc/spellcheck-enchant.h
+++ b/gcc/spellcheck-enchant.h
@@ -24,11 +24,10 @@ along with GCC; see the file COPYING3.  If not see
 
 extern void spellcheck_enchant_init (const char *language_tag);
 
-/* Callback for cpp_callbacks::comments for spellchecking comments
-   using Enchant.  */
+/* Spellcheck comments within TOK using Enchant.  */
 
-extern void spellcheck_enchant_check_comment (cpp_reader *, source_location,
-					      const unsigned char *, size_t);
+extern void spellcheck_enchant_check_comment (const cpp_token *token,
+					      location_t loc);
 
 extern void spellcheck_enchant_finish (void);
 
-- 
1.8.5.3

