From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#13244: cc-mode indentation fails depending on the phase of the moon Date: Mon, 11 Mar 2013 20:57:37 +0000 Message-ID: <20130311205737.GB24515@acm.acm> References: <7067FC50-22E1-4FF5-B634-A9062EF2849A@zdome.net> <20121227162054.GA3235@acm.acm> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1363035555 23194 80.91.229.3 (11 Mar 2013 20:59:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 11 Mar 2013 20:59:15 +0000 (UTC) Cc: 13244@debbugs.gnu.org To: Dietrich Epp Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Mar 11 21:59:39 2013 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UF9op-0001Ca-6o for geb-bug-gnu-emacs@m.gmane.org; Mon, 11 Mar 2013 21:59:39 +0100 Original-Received: from localhost ([::1]:42523 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UF9oT-0006bw-3e for geb-bug-gnu-emacs@m.gmane.org; Mon, 11 Mar 2013 16:59:17 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:50568) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UF9oO-0006bB-FJ for bug-gnu-emacs@gnu.org; Mon, 11 Mar 2013 16:59:14 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UF9oL-0001B8-6K for bug-gnu-emacs@gnu.org; Mon, 11 Mar 2013 16:59:12 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:42786) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UF9oJ-0001Am-1l; Mon, 11 Mar 2013 16:59:07 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.72) (envelope-from ) id 1UF9pC-0000xx-KP; Mon, 11 Mar 2013 17:00:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org, bug-cc-mode@gnu.org Resent-Date: Mon, 11 Mar 2013 21:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 13244 X-GNU-PR-Package: emacs,cc-mode X-GNU-PR-Keywords: Original-Received: via spool by 13244-submit@debbugs.gnu.org id=B13244.13630355523631 (code B ref 13244); Mon, 11 Mar 2013 21:00:02 +0000 Original-Received: (at 13244) by debbugs.gnu.org; 11 Mar 2013 20:59:12 +0000 Original-Received: from localhost ([127.0.0.1]:46894 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1UF9oN-0000wV-R3 for submit@debbugs.gnu.org; Mon, 11 Mar 2013 16:59:12 -0400 Original-Received: from colin.muc.de ([193.149.48.1]:54184 helo=mail.muc.de) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1UF9oJ-0000wL-UM for 13244@debbugs.gnu.org; Mon, 11 Mar 2013 16:59:10 -0400 Original-Received: (qmail 96057 invoked by uid 3782); 11 Mar 2013 20:58:10 -0000 Original-Received: from acm.muc.de (pD951B871.dip.t-dialin.net [217.81.184.113]) by colin.muc.de (tmda-ofmipd) with ESMTP; Mon, 11 Mar 2013 21:58:04 +0100 Original-Received: (qmail 26877 invoked by uid 1000); 11 Mar 2013 20:57:37 -0000 Content-Disposition: inline In-Reply-To: <20121227162054.GA3235@acm.acm> User-Agent: Mutt/1.5.21 (2010-09-15) X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:72342 Archived-At: Hello again, Dietrich. On Thu, Dec 27, 2012 at 04:20:54PM +0000, Alan Mackenzie wrote: > >This bug is weird. I couldn't use auto-indentation in a certain file. > >I tried restarting Emacs, behavior persists. I upgraded to the latest > >version from Bzr, behavior persists. I created a new account with no > >.emacs file and tried from there, behavior persists. In comparison, > >indentation works correctly in the Emacs that in the current Debian > >Wheezy repository, which is 24.3.50.1. > >Trying to automatically indent code inside the last function in the > >attached file fails. It succeeds if I make almost any modification to > >code above the last function in the file -- it could be something as > >minor as adding a space on an empty line, but changing text in the > >comments seems to have no effect. Indentation works in every function > >but the last one (or any one you create below it). > >The point at which indentation breaks is somewhere very close to the > >bottom of the file, I deleted code from the bottom of the file until I > >found the location where it breaks. > What is happening is this: CC Mode maintains a cache of "safe positions" > (i.e. positions not in a string or comment) approximately every 3000 > bytes. By pure chance, one of these supposed positions in your file is > at pos. 21002, which happens to be between the "/" and the "*" of the > comment opener on L836. This position isn't safe at all, and is found > due to a bug in a low level scanning routine (`parse-partial-sexp') in > Emacs. > Furthermore, that comment contains an "unbalanced" "'". So when CC Mode > scans forward from that supposed safe position, it finds the "'" and > thinks it's inside a string. This is why indentation isn't working in > your file after that comment. > As you've noted, the slightest change to the buffer and the error no > longer happens (since the "safe" position is no longer inside the > comment opener). > Thank you very much indeed for taking the trouble to report this arcane > bug. We actually noticed the possibility of this bug in December 2011 > and discussed it a bit, but nobody actually got around to fixing it > then. Maybe we should now. I've got a patch which should fix this problem. Maybe you could apply the patch to Emacs and check that it works properly. Just in case you don't have the critical file any more, I could send that to you too. :-) Again, thanks for the bug report. diff -r bc6aa6ba14cc cc-engine.el --- a/cc-engine.el Wed Mar 06 12:02:31 2013 +0000 +++ b/cc-engine.el Mon Mar 11 20:51:42 2013 +0000 @@ -2179,32 +2179,45 @@ ;; reduced by buffer changes, and increased by invocations of ;; `c-state-literal-at'. FIMXE!!! -(defsubst c-state-pp-to-literal (from to) +(defsubst c-state-pp-to-literal (from to &optional not-in-delimiter) ;; Do a parse-partial-sexp from FROM to TO, returning either ;; (STATE TYPE (BEG . END)) if TO is in a literal; or ;; (STATE) otherwise, ;; where STATE is the parsing state at TO, TYPE is the type of the literal ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal. ;; + ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character + ;; comment opener, this is recognized as being in a comment literal. + ;; ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of ;; STATE are valid. (save-excursion (let ((s (parse-partial-sexp from to)) - ty) - (when (or (nth 3 s) (nth 4 s)) ; in a string or comment + ty co-st) + (cond + ((or (nth 3 s) (nth 4 s)) ; in a string or comment (setq ty (cond ((nth 3 s) 'string) - ((eq (nth 7 s) t) 'c++) + ((nth 7 s) 'c++) (t 'c))) (parse-partial-sexp (point) (point-max) - nil ; TARGETDEPTH - nil ; STOPBEFORE - s ; OLDSTATE - 'syntax-table)) ; stop at end of literal - (if ty - `(,s ,ty (,(nth 8 s) . ,(point))) - `(,s))))) + nil ; TARGETDEPTH + nil ; STOPBEFORE + s ; OLDSTATE + 'syntax-table) ; stop at end of literal + `(,s ,ty (,(nth 8 s) . ,(point)))) + + ((and (not not-in-delimiter) ; inside a comment starter + (not (bobp)) + (progn (backward-char) + (looking-at c-comment-start-regexp))) + (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++) + co-st (point)) + (forward-comment 1) + `(,s ,ty (,co-st . ,(point)))) + + (t `(,s)))))) (defun c-state-safe-place (here) ;; Return a buffer position before HERE which is "safe", i.e. outside any @@ -3146,10 +3159,13 @@ ;; This function is called from c-after-change. ;; The caches of non-literals: - (if (< here c-state-nonlit-pos-cache-limit) - (setq c-state-nonlit-pos-cache-limit here)) - (if (< here c-state-semi-nonlit-pos-cache-limit) - (setq c-state-semi-nonlit-pos-cache-limit here)) + ;; Note that we use "<=" for the possibility of the second char of a two-char + ;; comment opener being typed; this would invalidate any cache position at + ;; HERE. + (if (<= here c-state-nonlit-pos-cache-limit) + (setq c-state-nonlit-pos-cache-limit (1- here))) + (if (<= here c-state-semi-nonlit-pos-cache-limit) + (setq c-state-semi-nonlit-pos-cache-limit (1- here))) ;; `c-state-cache': ;; Case 1: if `here' is in a literal containing point-min, everything @@ -4467,19 +4483,12 @@ (lim (or lim (c-state-semi-safe-place pos))) (pp-to-lit (save-restriction (widen) - (c-state-pp-to-literal lim pos))) + (c-state-pp-to-literal lim pos not-in-delimiter))) (state (car pp-to-lit)) (lit-limits (car (cddr pp-to-lit)))) (cond (lit-limits) - ((and (not not-in-delimiter) - (not (elt state 5)) - (eq (char-before) ?/) - (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28. - ;; We're standing in a comment starter. - (backward-char 1) - (cons (point) (progn (c-forward-single-comment) (point)))) (near (goto-char pos) diff -r bc6aa6ba14cc cc-fonts.el --- a/cc-fonts.el Wed Mar 06 12:02:31 2013 +0000 +++ b/cc-fonts.el Mon Mar 11 20:51:42 2013 +0000 @@ -2449,7 +2449,7 @@ (setq comment-beg nil)) (setq region-beg comment-beg)) - (if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t) + (if (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) ;; Collect a sequence of doc style line comments. (progn (goto-char comment-beg) -- Alan Mackenzie (Nuremberg, Germany).