bug#55181: CC Mode 5.35.1 (C/*l); c-guess-style not working

Alan Mackenzie <[email protected]> Fri, 29 Apr 2022 20:15:20 +0000
Newsgroups gmane.emacs.cc-mode.general
Message-ID <YmxHWIrhAVxbH0A9@ACM>
Hello, Po.

On Fri, Apr 29, 2022 at 17:02:45 +0800, Po Lu via CC-Mode-help wrote:
> Package: cc-mode

> Emacs  : GNU Emacs 29.0.50 (build 372, x86_64-pc-linux-gnu, Motif Version 2.3.4)
>  of 2022-04-29
> Package: CC Mode 5.35.1 (C/*l)
> Buffer Style: GNU
> c-emacs-features: (pps-extended-state col-0-paren posix-char-classes gen-string-delim gen-comment-delim syntax-properties 1-bit)

> Open the latest revision of xterm.c in the Emacs source tree.  Then type
> "M-x c-guess RET".  After a while, the guessing fails with:

>   c-syntactic-re-search-forward: Invalid search bound (wrong side of point)

Sorry about that.  It was caused by a bug in CC Mode's state-cache
handling functions.  I think the following patch fixes things.  Could
you try it out, please, and let me know how well it works.  Thanks!


diff -r 04b2d5e1eff8 cc-engine.el
--- a/cc-engine.el	Wed Apr 27 18:05:17 2022 +0000
+++ b/cc-engine.el	Fri Apr 29 19:56:29 2022 +0000
@@ -3431,7 +3431,9 @@
   ;; Return a good pos (in the sense of `c-state-cache-good-pos') at the
   ;; lowest[*] position between POS and HERE which is syntactically equivalent
   ;; to HERE.  This position may be HERE itself.  POS is before HERE in the
-  ;; buffer.
+  ;; buffer.  If POS and HERE are both in the same literal, return the start
+  ;; of the literal.  STATE is the parsing state at POS.
+  ;;
   ;; [*] We don't actually always determine this exact position, since this
   ;; would require a disproportionate amount of work, given that this function
   ;; deals only with a corner condition, and POS and HERE are typically on
@@ -3447,7 +3449,7 @@
 	  (setq pos (point)
 		state s)))
       (if (eq (point) here)		; HERE is in the same literal as POS
-	  pos
+	  (nth 8 state)		    ; A valid good pos cannot be in a literal.
 	(setq s (parse-partial-sexp pos here (1+ (car state)) nil state nil))
 	(cond
 	 ((> (car s) (car state))  ; Moved into a paren between POS and HERE

[ CC Mode configuration snipped, but appreciated. ]

-- 
Alan Mackenzie (Nuremberg, Germany).