GNU bug report logs - #47468
27.1; cc-mode: Got incorrect indentaton for C++ lambda function.

Previous Next

Package: emacs;

Reported by: Jianbin Kang <kjbmail <at> gmail.com>

Date: Mon, 29 Mar 2021 14:46:02 UTC

Severity: normal

Found in version 27.1

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 47468 in the body.
You can then email your comments to 47468 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#47468; Package emacs. (Mon, 29 Mar 2021 14:46:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Jianbin Kang <kjbmail <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Mon, 29 Mar 2021 14:46:02 GMT) Full text and rfc822 format available.

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

From: Jianbin Kang <kjbmail <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 27.1; cc-mode: Got incorrect indentaton for C++ lambda function.
Date: Mon, 29 Mar 2021 20:47:52 +0800
[Message part 1 (text/plain, inline)]
The c++ file to reproduce it:
```c++
#include <stdio.h>
#include <functional>

struct PP {
    const char *        name;
};

static void print(const char *name, std::function<void(const char *)> f)
{
    f(name);
}

int main(int argc, char *argv[])
{
    PP A = { "Jim" };
    PP *a = &A;

    print(A.name, [](const char *name) {
        printf("%s\n", name);           // Syntactic ((inlambda) ...
    });
    print(a->name, [=](const char *name) {
            printf("%s\n", name);       // Syntactic((arglist-cont-nonempty
...
        });

    return 0;
}

```

If I put cursor in lambda body of first print call, and run
'c-show-syntactic-information', it shows 'Syntactic analysis: ((inlambda)
(defun-block-intro 258))', which is correct and I get good indentation.

But in second print call, I get 'Syntactic analysis:
((arglist-cont-nonempty 337 342) (statement-block-intro 337))' and the
indentation is bad.

My emacs version is 27.1. I can reproduce the problem in both Linux and
Windows with 'emacs -Q'.
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#47468; Package emacs. (Wed, 12 May 2021 21:58:02 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Jianbin Kang <kjbmail <at> gmail.com>
Cc: Alan Mackenzie <acm <at> muc.de>, 47468 <at> debbugs.gnu.org
Subject: Re: bug#47468: 27.1; cc-mode: Got incorrect indentaton for C++
 lambda function.
Date: Wed, 12 May 2021 23:57:24 +0200
[Message part 1 (text/plain, inline)]
Jianbin Kang <kjbmail <at> gmail.com> writes:

> int main(int argc, char *argv[])
> {
>     PP A = { "Jim" };
>     PP *a = &A;
>
>     print(A.name, [](const char *name) {
>         printf("%s\n", name);           // Syntactic ((inlambda) ... 
>     });
>     print(a->name, [=](const char *name) {
>             printf("%s\n", name);       // Syntactic((arglist-cont-nonempty ...
>         });
>
>     return 0;

[...]

> If I put cursor in lambda body of first print call, and run 'c-show-syntactic-information',
> it shows 'Syntactic analysis: ((inlambda) (defun-block-intro 258))', which is correct and
> I get good indentation.
>
> But in second print call, I get 'Syntactic analysis:
> ((arglist-cont-nonempty 337 342) (statement-block-intro 337))' and the
> indentation is bad.

I can reproduce this in Emacs 28, too.  Poking around a bit, it seems
like it's getting tripped up by the "a->name" bit?  Changing that to
a.name (for instance) makes it recognise the second form as a lambda
function...

However, the first form is indented wrong for me in Emacs 28:

[Message part 2 (image/png, inline)]
[Message part 3 (text/plain, inline)]
This is indented correctly in Emacs 27.1, so it seems like this has
regressed between Emacs 27.1 and Emacs 28.

I've added 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#47468; Package emacs. (Sat, 15 May 2021 14:58:02 GMT) Full text and rfc822 format available.

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

From: Alan Mackenzie <acm <at> muc.de>
To: Jianbin Kang <kjbmail <at> gmail.com>
Cc: acm <at> muc.de, Lars Ingebrigtsen <larsi <at> gnus.org>, 47468 <at> debbugs.gnu.org
Subject: Re: bug#47468: 27.1; cc-mode: Got incorrect indentaton for C++
 lambda function.
Date: Sat, 15 May 2021 14:57:45 +0000
Hello, Jianbin.

Thank you indeed for taking the trouble to report this bug, and thank
you even more for cutting the test case down to a concise, easy to work
with file.

On Mon, Mar 29, 2021 at 20:47:52 +0800, Jianbin Kang wrote:
> The c++ file to reproduce it:
> ```c++
> #include <stdio.h>
> #include <functional>

> struct PP {
>     const char *        name;
> };

> static void print(const char *name, std::function<void(const char *)> f)
> {
>     f(name);
> }

> int main(int argc, char *argv[])
> {
>     PP A = { "Jim" };
>     PP *a = &A;

>     print(A.name, [](const char *name) {
>         printf("%s\n", name);           // Syntactic ((inlambda) ...
>     });
>     print(a->name, [=](const char *name) {
>             printf("%s\n", name);       // Syntactic((arglist-cont-nonempty
> ...
>         });

>     return 0;
> }

> ```

> If I put cursor in lambda body of first print call, and run
> 'c-show-syntactic-information', it shows 'Syntactic analysis: ((inlambda)
> (defun-block-intro 258))', which is correct and I get good indentation.

> But in second print call, I get 'Syntactic analysis:
> ((arglist-cont-nonempty 337 342) (statement-block-intro 337))' and the
> indentation is bad.

Yes.  What is happening is that CC Mode is falsely recognising the -> in
a->name as the trailing return type of the lambda expression.

I think the following patch should fix it.  Could I ask you, please, to
apply this patch to ..../emacs/lisp/progmodes/cc-engine.el, byte-compile
the file, and try it out on your real C++ source code.  In the unlikely
event you want help with the patching or byte-compilation, feel free to
send me private email.  After this, please either confirm to us that the
problem is fixed, or tell us what is still not working.  Thanks!



diff -r 92a4592886a1 cc-engine.el
--- a/cc-engine.el	Sun Apr 25 17:26:38 2021 +0000
+++ b/cc-engine.el	Sat May 15 14:51:56 2021 +0000
@@ -12269,7 +12269,7 @@
 		       (save-excursion
 			 (while
 			     (progn
-			       (c-syntactic-skip-backward "^;=}>" closest-lim t)
+			       (c-syntactic-skip-backward "^;=,}>" closest-lim t)
 			       (and (eq (char-before) ?>)
 				    (c-backward-token-2)
 				    (not (looking-at c-haskell-op-re)))))


> My emacs version is 27.1. I can reproduce the problem in both Linux and
> Windows with 'emacs -Q'.

-- 
Alan Mackenzie (Nuremberg, Germany).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#47468; Package emacs. (Wed, 19 May 2021 21:23:01 GMT) Full text and rfc822 format available.

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

From: Vladimir Lichevsky <vlichevsky <at> gmail.com>
To: 47468 <at> debbugs.gnu.org
Subject: Re: bug#47468: 27.1;
 cc-mode: Got incorrect indentaton for C++ lambda function.
Date: Wed, 19 May 2021 23:49:08 +0300
Not the original poster, but I tried this patch on recent build of
Emacs from master branch (commit
567c31121fdef6bdc8b645999a6ca1d994378c89) and it looks like the patch
fixes the bug.




Reply sent to Alan Mackenzie <acm <at> muc.de>:
You have taken responsibility. (Sun, 15 Aug 2021 19:52:01 GMT) Full text and rfc822 format available.

Notification sent to Jianbin Kang <kjbmail <at> gmail.com>:
bug acknowledged by developer. (Sun, 15 Aug 2021 19:52:01 GMT) Full text and rfc822 format available.

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

From: Alan Mackenzie <acm <at> muc.de>
To: Jianbin Kang <kjbmail <at> gmail.com>
Cc: acm <at> muc.de, Lars Ingebrigtsen <larsi <at> gnus.org>, 47468-done <at> debbugs.gnu.org
Subject: Re: bug#47468: 27.1; cc-mode: Got incorrect indentaton for C++
 lambda function.
Date: Sun, 15 Aug 2021 19:51:09 +0000
Hello, Jianbin and Lars.

Some while has passed since the bug was opened, and since the patch I
suggested seems to fix it, I am now closing this bug.

-- 
Alan Mackenzie (Nuremberg, Germany).


On Sat, May 15, 2021 at 14:57:45 +0000, Alan Mackenzie wrote:
> Hello, Jianbin.

> Thank you indeed for taking the trouble to report this bug, and thank
> you even more for cutting the test case down to a concise, easy to work
> with file.

> On Mon, Mar 29, 2021 at 20:47:52 +0800, Jianbin Kang wrote:
> > The c++ file to reproduce it:
> > ```c++
> > #include <stdio.h>
> > #include <functional>

> > struct PP {
> >     const char *        name;
> > };

> > static void print(const char *name, std::function<void(const char *)> f)
> > {
> >     f(name);
> > }

> > int main(int argc, char *argv[])
> > {
> >     PP A = { "Jim" };
> >     PP *a = &A;

> >     print(A.name, [](const char *name) {
> >         printf("%s\n", name);           // Syntactic ((inlambda) ...
> >     });
> >     print(a->name, [=](const char *name) {
> >             printf("%s\n", name);       // Syntactic((arglist-cont-nonempty
> > ...
> >         });

> >     return 0;
> > }

> > ```

> > If I put cursor in lambda body of first print call, and run
> > 'c-show-syntactic-information', it shows 'Syntactic analysis: ((inlambda)
> > (defun-block-intro 258))', which is correct and I get good indentation.

> > But in second print call, I get 'Syntactic analysis:
> > ((arglist-cont-nonempty 337 342) (statement-block-intro 337))' and the
> > indentation is bad.

> Yes.  What is happening is that CC Mode is falsely recognising the -> in
> a->name as the trailing return type of the lambda expression.

> I think the following patch should fix it.  Could I ask you, please, to
> apply this patch to ..../emacs/lisp/progmodes/cc-engine.el, byte-compile
> the file, and try it out on your real C++ source code.  In the unlikely
> event you want help with the patching or byte-compilation, feel free to
> send me private email.  After this, please either confirm to us that the
> problem is fixed, or tell us what is still not working.  Thanks!



> diff -r 92a4592886a1 cc-engine.el
> --- a/cc-engine.el	Sun Apr 25 17:26:38 2021 +0000
> +++ b/cc-engine.el	Sat May 15 14:51:56 2021 +0000
> @@ -12269,7 +12269,7 @@
>  		       (save-excursion
>  			 (while
>  			     (progn
> -			       (c-syntactic-skip-backward "^;=}>" closest-lim t)
> +			       (c-syntactic-skip-backward "^;=,}>" closest-lim t)
>  			       (and (eq (char-before) ?>)
>  				    (c-backward-token-2)
>  				    (not (looking-at c-haskell-op-re)))))


> > My emacs version is 27.1. I can reproduce the problem in both Linux and
> > Windows with 'emacs -Q'.

> -- 
> Alan Mackenzie (Nuremberg, Germany).




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Mon, 13 Sep 2021 11:24:07 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 198 days ago.

Previous Next


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