GNU bug report logs - #56256
Emacs 28.1 gets stuck when typing some C++ code

Previous Next

Package: emacs;

Reported by: Iru Cai <vimacs <at> disroot.org>

Date: Mon, 27 Jun 2022 12:32:02 UTC

Severity: normal

Tags: patch

Done: Alan Mackenzie <acm <at> muc.de>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 56256 in the body.
You can then email your comments to 56256 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#56256; Package emacs. (Mon, 27 Jun 2022 12:32:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Iru Cai <vimacs <at> disroot.org>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Mon, 27 Jun 2022 12:32:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Iru Cai <vimacs <at> disroot.org>
To: bug-gnu-emacs <at> gnu.org
Subject: Emacs 28.1 gets stuck when typing some C++ code
Date: Mon, 27 Jun 2022 18:22:15 +0800
Emacs gets stuck when trying to type the '&' character at the end of the 
buffer when there's the following code in C++ mode (this code is from 
https://www.gingerbill.org/article/2015/08/19/defer-in-cpp/):

```

template <typename F>
struct privDefer {
    F f;
    privDefer(F f) : f(f) {}
    ~privDefer() { f(); }
};

template <typename F>
privDefer<F> defer_func(F f) {
    return privDefer<F>(f);
}

#define DEFER_1(x, y) x##y
#define DEFER_2(x, y) DEFER_1(x, y)
#define DEFER_3(x)    DEFER_2(x, __COUNTER__)
#define defer(code) auto DEFER_3(_defer_) = defer_func([

```

Steps to reproduce:

1. create a C++ source file with this code and open it with Emacs, if 
the file doesn't have a C++ source suffix, enable c++mode

2. type the '&' symbol at the last line after "defer_func(["

3. then Emacs gets stuck

I can see this bug in Emacs 28.1 and git revision 
5b1bb1af030597aab7f7895b6e3da9b430f9438a. I've also tried ``emacs -Q`` 
and ``emacs -Q -nw``, the bug still exists.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#56256; Package emacs. (Tue, 28 Jun 2022 12:54:01 GMT) Full text and rfc822 format available.

Message #8 received at 56256 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Iru Cai <vimacs <at> disroot.org>
Cc: Alan Mackenzie <acm <at> muc.de>, 56256 <at> debbugs.gnu.org
Subject: Re: bug#56256: Emacs 28.1 gets stuck when typing some C++ code
Date: Tue, 28 Jun 2022 14:52:48 +0200
Iru Cai <vimacs <at> disroot.org> writes:

> 1. create a C++ source file with this code and open it with Emacs, if
> the file doesn't have a C++ source suffix, enable c++mode
>
> 2. type the '&' symbol at the last line after "defer_func(["
>
> 3. then Emacs gets stuck
>
> I can see this bug in Emacs 28.1 and git revision
> 5b1bb1af030597aab7f7895b6e3da9b430f9438a. I've also tried ``emacs -Q``
> and ``emacs -Q -nw``, the bug still exists.

Yup -- I can reproduce this on the current trunk, too -- and it hangs
Emacs hard: I have to kill Emacs after typing that "&".

I'm adding Alan to the CCs.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#56256; Package emacs. (Tue, 28 Jun 2022 16:29:01 GMT) Full text and rfc822 format available.

Message #11 received at 56256 <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Iru Cai <vimacs <at> disroot.org>
Cc: Lars Ingebrigtsen <larsi <at> gnus.org>, 56256 <at> debbugs.gnu.org
Subject: Re: bug#56256: Emacs 28.1 gets stuck when typing some C++ code
Date: Tue, 28 Jun 2022 16:28:27 +0000
Hello, Iru.

Thanks for taking the trouble to report this bug, and thanks even more
for such a concise and helpful report.

On Mon, Jun 27, 2022 at 18:22:15 +0800, Iru Cai wrote:
> Emacs gets stuck when trying to type the '&' character at the end of the 
> buffer when there's the following code in C++ mode (this code is from 
> https://www.gingerbill.org/article/2015/08/19/defer-in-cpp/):

> ```

> template <typename F>
> struct privDefer {
>      F f;
>      privDefer(F f) : f(f) {}
>      ~privDefer() { f(); }
> };

> template <typename F>
> privDefer<F> defer_func(F f) {
>      return privDefer<F>(f);
> }

> #define DEFER_1(x, y) x##y
> #define DEFER_2(x, y) DEFER_1(x, y)
> #define DEFER_3(x)    DEFER_2(x, __COUNTER__)
> #define defer(code) auto DEFER_3(_defer_) = defer_func([

> ```

> Steps to reproduce:

> 1. create a C++ source file with this code and open it with Emacs, if 
> the file doesn't have a C++ source suffix, enable c++mode

> 2. type the '&' symbol at the last line after "defer_func(["

> 3. then Emacs gets stuck

> I can see this bug in Emacs 28.1 and git revision 
> 5b1bb1af030597aab7f7895b6e3da9b430f9438a. I've also tried ``emacs -Q`` 
> and ``emacs -Q -nw``, the bug still exists.

Yes.  What is triggering the bug is the newly typed & being at the end of
a #define line.

The CC Mode function c-font-lock-c++-lambda-captures is in a loop,
handling one lambda capture at a time.  At the end of the file, it has
just gone forward over the whitespace, and wants to return to the &.
However it wrongly uses the function c-backward-token-2, which treats all
the macro lines as whitespace, hence ends up at the }, 6 lines earlier.
It then searches forward for the "next" [, and ends up at the same one it
was at before.  This loops infinitely.

I think the following patch should fix it.  Could you try it out on your
real C++ code, please, and tell us whether the bug is actually fixed.
(If you want any help with applying the patch or byte compiling CC Mode
afterwards, feel free to send me private email.):



diff -r 03c932b2922b cc-fonts.el
--- a/cc-fonts.el	Sat Jun 18 15:40:47 2022 +0000
+++ b/cc-fonts.el	Tue Jun 28 16:15:37 2022 +0000
@@ -1823,7 +1823,7 @@
   ;; font-lock-keyword-face.  It always returns NIL to inhibit this and
   ;; prevent a repeat invocation.  See elisp/lispref page "Search-based
   ;; Fontification".
-  (let (mode capture-default id-start id-end declaration sub-begin sub-end)
+  (let (mode capture-default id-start id-end declaration sub-begin sub-end tem)
     (while (and (< (point) limit)
 		(search-forward "[" limit t))
       (when (progn (backward-char)
@@ -1835,15 +1835,18 @@
 			(char-after)))
 	;; Is the first element of the list a bare "=" or "&"?
 	(when mode
-	  (forward-char)
-	  (c-forward-syntactic-ws)
-	  (if (memq (char-after) '(?, ?\]))
-	      (progn
-		(setq capture-default mode)
-		(when (eq (char-after) ?,)
-		  (forward-char)
-		  (c-forward-syntactic-ws)))
-	    (c-backward-token-2)))
+	  (setq tem nil)
+	  (save-excursion
+	    (forward-char)
+	    (c-forward-syntactic-ws)
+	    (if (memq (char-after) '(?, ?\]))
+		(progn
+		  (setq capture-default mode)
+		  (when (eq (char-after) ?,)
+		    (forward-char)
+		    (c-forward-syntactic-ws))
+		  (setq tem (point)))))
+	  (if tem (goto-char tem)))
 
 	;; Go round the following loop once per captured item.  We use "\\s)"
 	;; rather than "\\]" here to avoid infinite looping in this situation:


-- 
Alan Mackenzie (Nuremberg, Germany).




Added tag(s) patch. Request was from Stefan Kangas <stefan <at> marxist.se> to control <at> debbugs.gnu.org. (Tue, 28 Jun 2022 21:05:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#56256; Package emacs. (Wed, 29 Jun 2022 07:59:02 GMT) Full text and rfc822 format available.

Message #16 received at 56256 <at> debbugs.gnu.org (full text, mbox):

From: Iru Cai <vimacs <at> disroot.org>
To: Alan Mackenzie <acm <at> muc.de>
Cc: Lars Ingebrigtsen <larsi <at> gnus.org>, 56256 <at> debbugs.gnu.org
Subject: Re: bug#56256: Emacs 28.1 gets stuck when typing some C++ code
Date: Wed, 29 Jun 2022 11:03:47 +0800
On 2022/6/29 00:28, Alan Mackenzie wrote:
> I think the following patch should fix it.  Could you try it out on your
> real C++ code, please, and tell us whether the bug is actually fixed.
> (If you want any help with applying the patch or byte compiling CC Mode
> afterwards, feel free to send me private email.):

Thanks, this fixes this bug, and I haven't found a issue when editing 
some of my C++ code.

> diff -r 03c932b2922b cc-fonts.el
> --- a/cc-fonts.el	Sat Jun 18 15:40:47 2022 +0000
> +++ b/cc-fonts.el	Tue Jun 28 16:15:37 2022 +0000
> @@ -1823,7 +1823,7 @@
>     ;; font-lock-keyword-face.  It always returns NIL to inhibit this and
>     ;; prevent a repeat invocation.  See elisp/lispref page "Search-based
>     ;; Fontification".
> -  (let (mode capture-default id-start id-end declaration sub-begin sub-end)
> +  (let (mode capture-default id-start id-end declaration sub-begin sub-end tem)
>       (while (and (< (point) limit)
>   		(search-forward "[" limit t))
>         (when (progn (backward-char)
> @@ -1835,15 +1835,18 @@
>   			(char-after)))
>   	;; Is the first element of the list a bare "=" or "&"?
>   	(when mode
> -	  (forward-char)
> -	  (c-forward-syntactic-ws)
> -	  (if (memq (char-after) '(?, ?\]))
> -	      (progn
> -		(setq capture-default mode)
> -		(when (eq (char-after) ?,)
> -		  (forward-char)
> -		  (c-forward-syntactic-ws)))
> -	    (c-backward-token-2)))
> +	  (setq tem nil)
> +	  (save-excursion
> +	    (forward-char)
> +	    (c-forward-syntactic-ws)
> +	    (if (memq (char-after) '(?, ?\]))
> +		(progn
> +		  (setq capture-default mode)
> +		  (when (eq (char-after) ?,)
> +		    (forward-char)
> +		    (c-forward-syntactic-ws))
> +		  (setq tem (point)))))
> +	  (if tem (goto-char tem)))
>   
>   	;; Go round the following loop once per captured item.  We use "\\s)"
>   	;; rather than "\\]" here to avoid infinite looping in this situation:
>
>




Reply sent to Alan Mackenzie <acm <at> muc.de>:
You have taken responsibility. (Sat, 02 Jul 2022 16:24:02 GMT) Full text and rfc822 format available.

Notification sent to Iru Cai <vimacs <at> disroot.org>:
bug acknowledged by developer. (Sat, 02 Jul 2022 16:24:02 GMT) Full text and rfc822 format available.

Message #21 received at 56256-done <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Iru Cai <vimacs <at> disroot.org>
Cc: acm <at> muc.de, Lars Ingebrigtsen <larsi <at> gnus.org>, 56256-done <at> debbugs.gnu.org
Subject: Re: bug#56256: Emacs 28.1 gets stuck when typing some C++ code
Date: Sat, 2 Jul 2022 16:23:15 +0000
Hello, Iru.

On Wed, Jun 29, 2022 at 11:03:47 +0800, Iru Cai wrote:

> On 2022/6/29 00:28, Alan Mackenzie wrote:
> > I think the following patch should fix it.  Could you try it out on your
> > real C++ code, please, and tell us whether the bug is actually fixed.
> > (If you want any help with applying the patch or byte compiling CC Mode
> > afterwards, feel free to send me private email.):

> Thanks, this fixes this bug, and I haven't found a issue when editing 
> some of my C++ code.

[ .... ]

Thanks for the testing!

I've committed the patch to the release branch of our git tree, and I'm
closing the bug with this post.

-- 
Alan Mackenzie (Nuremberg, Germany).




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 31 Jul 2022 11:24:09 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 267 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.