From e726f2058c98e68c951bdb290fe68dac2a84ff65 Mon Sep 17 00:00:00 2001
From: Alan Mackenzie <acm@muc.de>
Date: Sat, 31 Jan 2015 21:44:47 +0000
Subject: [PATCH] Handle "#" operator properly inside macro.  Fix coding bug.

cc-mode.el (c-neutralize-syntax-in-and-mark-CPP): On finding a "#" which
looks like the start of a macro, check it isn't already inside a macro.

cc-engine.el (c-state-safe-place): Don't record a new "safe" position
into the list of them when this is beyond our current position.
---
 lisp/ChangeLog              | 12 ++++++++++++
 lisp/progmodes/cc-engine.el | 10 +++++++---
 lisp/progmodes/cc-mode.el   | 17 +++++++++++------
 3 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 6cdaf14abe2..fd54c688640 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,15 @@
+2015-01-31  Alan Mackenzie  <acm@muc.de>
+
+	Handle "#" operator properly inside macro.  Fix coding bug.
+
+	* progmodes/cc-mode.el (c-neutralize-syntax-in-and-mark-CPP): On
+	finding a "#" which looks like the start of a macro, check it
+	isn't already inside a macro.
+
+	* progmodes/cc-engine.el (c-state-safe-place): Don't record a new
+	"safe" position into the list of them when this is beyond our
+	current position.
+
 2015-01-31  Martin Rudalics  <rudalics@gmx.at>
 
 	* menu-bar.el (menu-bar-non-minibuffer-window-p): Return nil when
diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index 1f4aa819a53..b8051b274d2 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -2275,7 +2275,9 @@ comment at the start of cc-engine.el for more info."
 	  (while
 	      ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
 	      (and
-	       (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
+	       (setq npos
+		     (when (<= (+ pos c-state-nonlit-pos-interval) here)
+		       (+ pos c-state-nonlit-pos-interval)))
 
 	       ;; Test for being in a literal.  If so, go to after it.
 	       (progn
@@ -2302,7 +2304,9 @@ comment at the start of cc-engine.el for more info."
 	  ;; Add one extra element above HERE so as to to avoid the previous
 	  ;; expensive calculation when the next call is close to the current
 	  ;; one.  This is especially useful when inside a large macro.
-	  (setq c-state-nonlit-pos-cache (cons npos c-state-nonlit-pos-cache)))
+	  (when npos
+	    (setq c-state-nonlit-pos-cache
+		  (cons npos c-state-nonlit-pos-cache))))
 
 	(if (> pos c-state-nonlit-pos-cache-limit)
 	    (setq c-state-nonlit-pos-cache-limit pos))
@@ -3066,7 +3070,7 @@ comment at the start of cc-engine.el for more info."
       (setq dropped-cons (consp (car c-state-cache)))
       (setq c-state-cache (cdr c-state-cache))
       (setq pos pa))
-    ;; At this stage, (> pos here);
+    ;; At this stage, (>= pos here);
     ;; (< (c-state-cache-top-lparen) here)  (or is nil).
 
     (cond
diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
index c4f1efbfdb1..1cba5027f28 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -957,12 +957,17 @@ Note that the style variables are always made local to the buffer."
     (let ((pps-position (point))  pps-state mbeg)
       (while (and (< (point) c-new-END)
 		  (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
-	;; If we've found a "#" inside a string/comment, ignore it.
-	(setq pps-state
-	      (parse-partial-sexp pps-position (point) nil nil pps-state)
-	      pps-position (point))
-	(unless (or (nth 3 pps-state)	; in a string?
-		    (nth 4 pps-state))	; in a comment?
+	;; If we've found a "#" inside a macro/string/comment, ignore it.
+	(unless
+	    (or (save-excursion
+		  (goto-char (match-beginning 0))
+		  (c-beginning-of-macro))
+		(progn
+		  (setq pps-state
+			(parse-partial-sexp pps-position (point) nil nil pps-state)
+			pps-position (point))
+		  (or (nth 3 pps-state)	   ; in a string?
+		      (nth 4 pps-state)))) ; in a comment?
 	  (goto-char (match-beginning 1))
 	  (setq mbeg (point))
 	  (if (> (c-syntactic-end-of-macro) mbeg)
-- 
2.39.5