From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#61436: Emacs Freezing With Java Files Date: Fri, 13 Oct 2023 12:41:57 +0000 Message-ID: References: <835yc6hl0c.fsf@gnu.org> <87bkd7fsp4.fsf@sappc2.fritz.box> <87il7ew5wx.fsf@sappc2.fritz.box> <87il7dbosk.fsf@lidells.se> <87r0m1t0el.fsf@sappc2.fritz.box> <875y3bbokx.fsf@sappc2.fritz.box> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="Hf/FvbKl43qLgJz6" Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4589"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Robert Weiner , Hank Greenburg , Mats Lidell , Eli Zaretskii , rswgnu@gmail.com, 61436@debbugs.gnu.org To: Jens Schmidt Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Oct 13 14:42:58 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qrHVK-0000um-Dz for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 13 Oct 2023 14:42:58 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qrHV3-0006XF-PO; Fri, 13 Oct 2023 08:42:43 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qrHV1-0006WF-4w for bug-gnu-emacs@gnu.org; Fri, 13 Oct 2023 08:42:39 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qrHV0-0007LD-St for bug-gnu-emacs@gnu.org; Fri, 13 Oct 2023 08:42:38 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qrHVN-0003iW-Rg for bug-gnu-emacs@gnu.org; Fri, 13 Oct 2023 08:43:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 13 Oct 2023 12:43:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61436 X-GNU-PR-Package: emacs Original-Received: via spool by 61436-submit@debbugs.gnu.org id=B61436.169720095414252 (code B ref 61436); Fri, 13 Oct 2023 12:43:01 +0000 Original-Received: (at 61436) by debbugs.gnu.org; 13 Oct 2023 12:42:34 +0000 Original-Received: from localhost ([127.0.0.1]:45444 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qrHUv-0003hn-US for submit@debbugs.gnu.org; Fri, 13 Oct 2023 08:42:34 -0400 Original-Received: from mail.muc.de ([193.149.48.3]:51784) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qrHUr-0003hU-9L for 61436@debbugs.gnu.org; Fri, 13 Oct 2023 08:42:32 -0400 Original-Received: (qmail 7041 invoked by uid 3782); 13 Oct 2023 14:42:00 +0200 Original-Received: from acm.muc.de (pd953a199.dip0.t-ipconnect.de [217.83.161.153]) (using STARTTLS) by colin.muc.de (tmda-ofmipd) with ESMTP; Fri, 13 Oct 2023 14:41:58 +0200 Original-Received: (qmail 3694 invoked by uid 1000); 13 Oct 2023 12:41:57 -0000 Content-Disposition: inline In-Reply-To: <875y3bbokx.fsf@sappc2.fritz.box> X-Submission-Agent: TMDA/1.3.x (Ph3nix) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:272345 Archived-At: --Hf/FvbKl43qLgJz6 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Hello, Jens. On Thu, Oct 12, 2023 at 21:58:06 +0200, Jens Schmidt wrote: > Hi Alan, > Alan Mackenzie writes: [ .... ] > >> That always freezes Emacs (29 and master) even before it has a chance to > >> display P1.java. The freeze happens in function > >> `c-get-fallback-scan-pos', where the while loop inf-loops. Yes. > > c-get-fallback-scan-pos tries to move to the beginning of a function. > > This probably involves defun-prompt-regexp when it is non-nil. :-( > Otherwise we would see hangs or exponential behavior (?) somewhere in > the Emacs regexp machinerie, but they take place in that while loop. So > I guess that there must be some other, additional quality that this > regexp fulfills. Like: "matches the empty string" (which it does not, > as far as I can tell) or: "must only match before curlies" or whatnot. > Unfortunately, the doc string/info doc of `defun-prompt-regexp´ provides > only exactly that latter criterion: > That is to say, a defun begins on a line that starts with a match for > this regular expression, followed by a character with open-parenthesis > syntax. > I guess that only pruning that regexp until things start unfreezing > could give an answer here. Or more tracing to see how point moves in > `c-get-fallback-scan-pos'. But I need some tracing break here ... > ... or so I thought, I just couldn't resist: > I expanded and instrumented that function from emacs-29 as follows, > (hopefully) not changing any of its logic: > ------------------------- snip ------------------------- > (defun c-get-fallback-scan-pos (here) > ;; Return a start position for building `c-state-cache' from scratch. This > ;; will be at the top level, 2 defuns back. Return nil if we don't find > ;; these defun starts a reasonable way back. > (message "c-get-fallback-scan-pos") > (save-excursion > (save-restriction > (when (> here (* 10 c-state-cache-too-far)) > (narrow-to-region (- here (* 10 c-state-cache-too-far)) here)) > ;; Go back 2 bods, but ignore any bogus positions returned by > ;; beginning-of-defun (i.e. open paren in column zero). > (goto-char here) > (let ((cnt 2)) > (message "beginning-of-defun-loop-00: %d %d" cnt (point)) > (while (not (or (bobp) (zerop cnt))) > (message "beginning-of-defun-loop-01: %d" (point)) > (let (beginning-of-defun-function end-of-defun-function) > (beginning-of-defun)) > (and defun-prompt-regexp > (looking-at defun-prompt-regexp) > (message "beginning-of-defun-loop-02: %d" (point)) > (goto-char (match-end 0))) > (message "beginning-of-defun-loop-03: %d" (point)) > (if (eq (char-after) ?\{) > (setq cnt (1- cnt))))) > (and (not (bobp)) > (point))))) > ------------------------- snip ------------------------- > That results in the message triple > ------------------------- snip ------------------------- > beginning-of-defun-loop-01: 5879 > beginning-of-defun-loop-02: 5801 > beginning-of-defun-loop-03: 5879 > beginning-of-defun-loop-01: 5879 > beginning-of-defun-loop-02: 5801 > beginning-of-defun-loop-03: 5879 > ... > ------------------------- snip ------------------------- > inf-looping. These points are (|: 5801, ^: 5879) here in P1.java: > ------------------------- snip ------------------------- > 178 } catch (Exception e) { > 179| error("symTable.addDecl", "unexpected error with a single HashMap " + e)^; > 180 } > 181 > ------------------------- snip ------------------------- > So the catch-block just before line 181 is recognized as a potential BOD > (previous trailing open curly?). But then `defun-prompt-regexp' matches > the function call in the catch-block as defun prompt regexp (which it > better should not?), taking point back to where, on next BOD search, the > exact previous BOD is found again. > So probably there are really two issues here: > 1. The `defun-prompt-regexp' used by Hyperbole, which matches too > broadly, and > 2. function `c-get-fallback-scan-pos', which could try harder to avoid > inf-loops when such things happen. > But that's where I *really* stop here :-) You've diagnosed the bug completely. Thanks! The hang was caused entirely by the loop in c-get-fallback-scan-pos, not the deficiencies in that long regexp. defun-prompt-regexp, when appended with a \\s( (as is done in beginning-of-defun-raw) matches the " error(" on L179 of P1.java. The bare defun-prompt-regexp (as used in CC Mode) matches the entire line except the terminating ;. This regexp could do with some amendment, but it is not the main cause of the bug. To solve the bug, I'm amending the macro c-beginning-of-defun-1 so that it only stops at a debug-prompt-regexp position when it also found a {. Otherwise it will keep looping until it finds a better position or BOB. Would all concerned please apply the attached patch to the Emacs master branch, directory lisp/progmodes. Then please byte compile CC Mode in full (a macro has been changed), and try the result on your real Java code. (If anybody wants any help applying the patch or byte compiling, feel free to send me private mail.) Then please confirm that the bug is indeed fixed. Thanks! -- Alan Mackenzie (Nuremberg, Germany). --Hf/FvbKl43qLgJz6 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="diff.20231013.diff" diff -r b680bbba3141 cc-defs.el --- a/cc-defs.el Fri Sep 29 11:15:58 2023 +0000 +++ b/cc-defs.el Fri Oct 13 12:23:11 2023 +0000 @@ -944,7 +944,8 @@ (when dest (goto-char dest) t))) (defmacro c-beginning-of-defun-1 () - ;; Wrapper around beginning-of-defun. + ;; Wrapper around beginning-of-defun. Note that the return value from this + ;; macro has no significance. ;; ;; NOTE: This function should contain the only explicit use of ;; beginning-of-defun in CC Mode. Eventually something better than @@ -957,44 +958,49 @@ ;; `c-parse-state'. `(progn - (if (and ,(fboundp 'buffer-syntactic-context-depth) - c-enable-xemacs-performance-kludge-p) - ,(when (fboundp 'buffer-syntactic-context-depth) - ;; XEmacs only. This can improve the performance of - ;; c-parse-state to between 3 and 60 times faster when - ;; braces are hung. It can also degrade performance by - ;; about as much when braces are not hung. - '(let (beginning-of-defun-function end-of-defun-function - pos) - (while (not pos) - (save-restriction - (widen) - (setq pos (c-safe-scan-lists - (point) -1 (buffer-syntactic-context-depth)))) - (cond - ((bobp) (setq pos (point-min))) - ((not pos) - (let ((distance (skip-chars-backward "^{"))) - ;; unbalanced parenthesis, while invalid C code, - ;; shouldn't cause an infloop! See unbal.c - (when (zerop distance) - ;; Punt! - (beginning-of-defun) - (setq pos (point))))) - ((= pos 0)) - ((not (eq (char-after pos) ?{)) - (goto-char pos) - (setq pos nil)) - )) - (goto-char pos))) - ;; Emacs, which doesn't have buffer-syntactic-context-depth - (let (beginning-of-defun-function end-of-defun-function) - (beginning-of-defun))) - ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the - ;; open brace. - (and defun-prompt-regexp - (looking-at defun-prompt-regexp) - (goto-char (match-end 0))))) + (while + (progn + (if (and ,(fboundp 'buffer-syntactic-context-depth) + c-enable-xemacs-performance-kludge-p) + ,(when (fboundp 'buffer-syntactic-context-depth) + ;; XEmacs only. This can improve the performance of + ;; c-parse-state to between 3 and 60 times faster when + ;; braces are hung. It can also degrade performance by + ;; about as much when braces are not hung. + '(let (beginning-of-defun-function end-of-defun-function + pos) + (while (not pos) + (save-restriction + (widen) + (setq pos (c-safe-scan-lists + (point) -1 (buffer-syntactic-context-depth)))) + (cond + ((bobp) (setq pos (point-min))) + ((not pos) + (let ((distance (skip-chars-backward "^{"))) + ;; unbalanced parenthesis, while invalid C code, + ;; shouldn't cause an infloop! See unbal.c + (when (zerop distance) + ;; Punt! + (beginning-of-defun) + (setq pos (point))))) + ((= pos 0)) + ((not (eq (char-after pos) ?{)) + (goto-char pos) + (setq pos nil)) + )) + (goto-char pos))) + ;; Emacs, which doesn't have buffer-syntactic-context-depth + (let (beginning-of-defun-function end-of-defun-function) + (beginning-of-defun))) + (and (not (bobp)) + ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at + ;; the open brace. + defun-prompt-regexp + (looking-at (concat defun-prompt-regexp "\\s(")) + (or (not (eq (char-before (match-end 0)) ?{)) + (progn (goto-char (1- (match-end 0))) + nil))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; --Hf/FvbKl43qLgJz6--