Subject: Re: bug#11749: Acknowledgement (24.1; C-mode indentation gives wrong-type-argument error.)

From: Alan Mackenzie <[email-masked]>
Date: Sun, 7 Oct 2012 10:59:51 +0000

Hi, Michael.

On Fri, Sep 21, 2012 at 01:47:15PM -0400, Michael Welsh Duggan wrote:
> Michael Welsh Duggan <[email-masked]> writes:

> > Michael Welsh Duggan <[email-masked]> writes:

> >>>> There is generally no way to re-create it, so I've stopped reporting
> >>>> these.  I wish there were some way to record all actions in c-mode
> >>>> buffers such that they could be saved and re-played when this type of
> >>>> problem happens.  If there were some sort of debug flag I could turn
> >>>> on, I would turn it on by default and hopefully be able to catch some
> >>>> useful information.

> >>> M-x c-toggle-parse-state-debug

> >>> (or (c-toggle-parse-state-debug 1) in your .emacs).  Warning: variable
> >>> `c-debug-parse-state' is not buffer local.  I wrote this on 19th October
> >>> last year to help sort out the bug you reported a little earlier.  :-)

> >>> It works by calculating c-parse-state twice for each call - The first
> >>> time normally, then again with the internal state bound to "newly
> >>> initialised".  If the the two results differ, they are printed to
> >>> *Messages*, together with the saved previous state.  If this does
> >>> trigger, please note any recent buffer changes.  It may make editing
> >>> intolerably slow.

> >> I will turn this on.

> I turned this on, and inserted a (ding) where cc-mode outputs an
> inconsistency message.  I can barely stand to edit code now, due to
> what seem to be almost every key press causing a beep.  Have you made
> any progress here, or do you need more data?

I have found the bug which is causing (most of) these dings, though I
don't think it is the one which caused Kim's original bug.  Could you try
out the patch below, please.  (I have also enhanced/corrected the
debugging routines a bit, too.)

diff -r ac6584d14c06 cc-engine.el
--- a/cc-engine.el	Sun Sep 09 11:15:13 2012 +0000
+++ b/cc-engine.el	Sun Oct 07 10:54:51 2012 +0000
@@ -2648,17 +2648,19 @@
 	;; If we're essentially repeating a fruitless search, just give up.
 	(unless (and c-state-brace-pair-desert
 		     (eq cache-pos (car c-state-brace-pair-desert))
+		     (> from (car c-state-brace-pair-desert))
 		     (<= from (cdr c-state-brace-pair-desert)))
-	  ;; DESERT-LIM.  Only search what we absolutely need to,
+	  ;; DESERT-LIM.  Avoid repeated searching through the cached desert.
 	  (let ((desert-lim
 		 (and c-state-brace-pair-desert
 		      (eq cache-pos (car c-state-brace-pair-desert))
+		      (>= from (cdr c-state-brace-pair-desert))
 		      (cdr c-state-brace-pair-desert)))
 		;; CACHE-LIM.  This limit will be necessary when an opening
 		;; paren at `cache-pos' has just had its matching close paren
-		;; inserted.  `cache-pos' continues to be a search bound, even
-		;; though the algorithm below would skip over the new paren
-		;; pair.
+		;; inserted into the buffer.  `cache-pos' continues to be a
+		;; search bound, even though the algorithm below would skip
+		;; over the new paren pair.
 		(cache-lim (and cache-pos (< cache-pos from) cache-pos)))
 	    (narrow-to-region
 		(cond
@@ -3354,13 +3356,19 @@
   (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
 (cc-bytecomp-defun c-real-parse-state)
 
+(defvar c-parse-state-point nil)
 (defvar c-parse-state-state nil)
 (make-variable-buffer-local 'c-parse-state-state)
 (defun c-record-parse-state-state ()
+  (setq c-parse-state-point (point))
   (setq c-parse-state-state
 	(mapcar
 	 (lambda (arg)
-	   (cons arg (symbol-value arg)))
+	   (let ((val (symbol-value arg)))
+	     (cons arg
+		   (if (consp val)
+		       (copy-tree val)
+		     val))))
 	 '(c-state-cache
 	   c-state-cache-good-pos
 	   c-state-nonlit-pos-cache
@@ -3373,7 +3381,8 @@
 	   c-state-point-min-lit-start
 	   c-state-min-scan-pos
 	   c-state-old-cpp-beg
-	   c-state-old-cpp-end))))
+	   c-state-old-cpp-end
+	   c-parse-state-point))))
 (defun c-replay-parse-state-state ()
   (message
    (concat "(setq "
@@ -3416,7 +3425,8 @@
       (message "Old state:")
       (c-replay-parse-state-state))
     (c-record-parse-state-state)
-    res1))
+    res2 ; res1 correct a cascading series of errors ASAP
+    ))
 
 (defun c-toggle-parse-state-debug (&optional arg)
   (interactive "P")

> -- 
> Michael Welsh Duggan
> ([email-masked])

-- 
Alan Mackenzie (Nuremberg, Germany).