Subject: Re: bug#18749: 24.3.94; CC Mode 5.32.5 (C/l); cc-mode state cache failure leading to bad indentation

From: Alan Mackenzie <[email-masked]>
Date: Fri, 17 Oct 2014 20:12:05 +0000

Hello, Michael.

On Fri, Oct 17, 2014 at 06:19:50PM -0000, Alan Mackenzie wrote:

> > A consistent c-mode indentation error that causes bad indentation, much
> > cursing, and gnashing of teeth.  This occurs in the latest emacs-24
> > branch (and trunk).

[ .... ]

> >     M-x c-toggle-parse-state-debug
> >     C-x C-f rwuniqsetup.c.before.2
> >     C-s ipaddr_rec_to_bin RET
> >     C-v

> > At this point, indentation is having problems.  Try hitting TAB on line
> > 1371, for example.  Or line 1374 for a complete failure to indent at
> > all.

[ .... ]

> Look forward to a patch soon!

OK, Could you try this, please, and get back to me ASAP.  Thanks!

diff -r a785658eaedb cc-engine.el
--- a/cc-engine.el	Thu Oct 02 20:17:43 2014 +0000
+++ b/cc-engine.el	Fri Oct 17 19:58:19 2014 +0000
@@ -254,6 +254,24 @@
     (setq c-macro-cache-start-pos beg
 	  c-macro-cache-syntactic nil))))
 
+(defun c-macro-is-genuine-p ()
+  ;; Check that the ostensible CPP construct at point is a real one.  In
+  ;; particular, if point is on the first line of a narrowed buffer, make sure
+  ;; that the "#" isn't, say, the second character of a "##" operator.  Return
+  ;; t when the macro is real, nil otherwise.
+  (let ((here (point)))
+    (beginning-of-line)
+    (prog1
+	(if (and (eq (point) (point-min))
+		 (/= (point) 1))
+	    (save-restriction
+	      (widen)
+	      (beginning-of-line)
+	      (and (looking-at c-anchored-cpp-prefix)
+		   (eq (match-beginning 1) here)))
+	  t)
+      (goto-char here))))
+
 (defun c-beginning-of-macro (&optional lim)
   "Go to the beginning of a preprocessor directive.
 Leave point at the beginning of the directive and return t if in one,
@@ -284,7 +302,8 @@
 	    (forward-line -1))
 	  (back-to-indentation)
 	  (if (and (<= (point) here)
-		   (looking-at c-opt-cpp-start))
+		   (looking-at c-opt-cpp-start)
+		   (c-macro-is-genuine-p))
 	      (progn
 		(setq c-macro-cache (cons (point) nil)
 		      c-macro-cache-start-pos here)

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

-- 
Alan Mackenzie (Nuremberg, Germany).