unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type
@ 2023-05-04 21:54 Olivier Certner
       [not found] ` <handler.63285.B.168323729911280.ack@debbugs.gnu.org>
  2023-05-04 22:14 ` bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type Olivier Certner
  0 siblings, 2 replies; 8+ messages in thread
From: Olivier Certner @ 2023-05-04 21:54 UTC (permalink / raw)
  To: 63285

[-- Attachment #1: Type: text/plain, Size: 163 bytes --]

Hello,

See attached 2-line reproducer.  E.g., type `C-c C-s' on the second line.

Fix to be attached as soon as the bug is created.

Regards.

-- 
Olivier Certner

[-- Attachment #2: test.c --]
[-- Type: text/x-csrc, Size: 59 bytes --]

PROD_TYPE(element) element_list;
                   int a;

^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#63285: Acknowledgement (30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type)
       [not found] ` <handler.63285.B.168323729911280.ack@debbugs.gnu.org>
@ 2023-05-04 21:57   ` Olivier Certner
  0 siblings, 0 replies; 8+ messages in thread
From: Olivier Certner @ 2023-05-04 21:57 UTC (permalink / raw)
  To: 63285

[-- Attachment #1: Type: text/plain, Size: 77 bytes --]

Proposed fix.  Applies on master (b28d44d4226497c4b258).

-- 
Olivier Certner

[-- Attachment #2: 0001-CC-Mode-Fix-K-R-argument-declaration-misdetection-af.patch --]
[-- Type: text/x-patch, Size: 2068 bytes --]

From 249226b450b88c423b8d395c220ddf5a00ef9332 Mon Sep 17 00:00:00 2001
From: Olivier Certner <olce.emacs@certner.fr>
Date: Wed, 3 May 2023 11:44:43 +0200
Subject: [PATCH] CC Mode: Fix K&R argument declaration misdetection after
 parenthesized type

* lisp/progmodes/cc-engine.el (c-in-knr-argdecl): When trying to loop
over candidate declarations (between the function declaration's
identifier list's end and point) to check whether the names of their
identifiers correspond with that of the identifier list, actually fail
as soon as stumbling on something other than a declaration instead of
silently succeeding, which causes some constructs to be erroneously
recognized as K&R argument declarations.

(Bug#63285)
---
 lisp/progmodes/cc-engine.el | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index 8b34daf03c2..0d9f0fec03b 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -12039,17 +12039,25 @@ c-in-knr-argdecl
 		       ;; Each time around the following checks one
 		       ;; declaration (which may contain several identifiers).
 		       (while (and
-			       (consp (setq decl-or-cast
+			       (or
+				(and
+				 (consp
+				  (setq decl-or-cast
 					    (c-forward-decl-or-cast-1
 					     after-prec-token
 					     nil ; Or 'arglist ???
 					     nil)))
-			       (memq (char-after) '(?\; ?\,))
-			       (goto-char (car decl-or-cast))
-			       (save-excursion
-				 (setq semi-position+1
-				       (c-syntactic-re-search-forward
-					";" (+ (point) 1000) t)))
+				 (memq (char-after) '(?\; ?\,))
+				 (goto-char (car decl-or-cast))
+				 (save-excursion
+				   (setq semi-position+1
+					 (1+ (or
+					      (c-syntactic-re-search-forward
+					       ";" (point) t)
+					      (1- (point-max)))))))
+				;; Can't parse declarations correctly,
+				;; bail out.
+				(throw 'knr nil))
 			       (c-do-declarators
 				semi-position+1 t nil nil
 				(lambda (id-start id-end _next _not-top
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type
  2023-05-04 21:54 bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type Olivier Certner
       [not found] ` <handler.63285.B.168323729911280.ack@debbugs.gnu.org>
@ 2023-05-04 22:14 ` Olivier Certner
  2023-10-05 20:43   ` Stefan Kangas
  1 sibling, 1 reply; 8+ messages in thread
From: Olivier Certner @ 2023-05-04 22:14 UTC (permalink / raw)
  To: 63285

[-- Attachment #1: Type: text/plain, Size: 124 bytes --]

Sorry, I had clobbered the "(+ (point) 1000)" part in the previous patch.

Please use this one instead.

-- 
Olivier Certner

[-- Attachment #2: 0001-CC-Mode-Fix-K-R-argument-declaration-misdetection-af.patch --]
[-- Type: text/x-patch, Size: 2201 bytes --]

From 0515de23d84a48c57b456c6730f826c5d783b965 Mon Sep 17 00:00:00 2001
From: Olivier Certner <olce.emacs@certner.fr>
Date: Wed, 3 May 2023 11:44:43 +0200
Subject: [PATCH] CC Mode: Fix K&R argument declaration misdetection after
 parenthesized type

* lisp/progmodes/cc-engine.el (c-in-knr-argdecl): When trying to loop
over candidate declarations (between the function declaration's
identifier list's end and point) to check whether the names of their
identifiers correspond with that of the identifier list, actually fail
as soon as stumbling on something other than a declaration instead of
silently succeeding, which causes some constructs to be erroneously
recognized as K&R argument declarations.

(Bug#63285)
---
 lisp/progmodes/cc-engine.el | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index 8b34daf03c2..27740b4903c 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -12039,17 +12039,24 @@ c-in-knr-argdecl
 		       ;; Each time around the following checks one
 		       ;; declaration (which may contain several identifiers).
 		       (while (and
-			       (consp (setq decl-or-cast
-					    (c-forward-decl-or-cast-1
-					     after-prec-token
-					     nil ; Or 'arglist ???
-					     nil)))
-			       (memq (char-after) '(?\; ?\,))
-			       (goto-char (car decl-or-cast))
-			       (save-excursion
-				 (setq semi-position+1
-				       (c-syntactic-re-search-forward
-					";" (+ (point) 1000) t)))
+			       (or
+				(and
+				 (consp (setq decl-or-cast
+					      (c-forward-decl-or-cast-1
+					       after-prec-token
+					       nil ; Or 'arglist ???
+					       nil)))
+				 (memq (char-after) '(?\; ?\,))
+				 (goto-char (car decl-or-cast))
+				 (save-excursion
+				   (setq semi-position+1
+					 (1+ (or
+					      (c-syntactic-re-search-forward
+					       ";" (+ (point) 1000) t)
+					      (1- (point-max)))))))
+				;; Can't parse declarations correctly,
+				;; bail out.
+				(throw 'knr nil))
 			       (c-do-declarators
 				semi-position+1 t nil nil
 				(lambda (id-start id-end _next _not-top
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type
  2023-05-04 22:14 ` bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type Olivier Certner
@ 2023-10-05 20:43   ` Stefan Kangas
  2023-10-06  9:09     ` Alan Mackenzie
  2023-10-06 13:18     ` Alan Mackenzie
  0 siblings, 2 replies; 8+ messages in thread
From: Stefan Kangas @ 2023-10-05 20:43 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: 63285, Olivier Certner

Olivier Certner <ocert.dev@free.fr> writes:

> Sorry, I had clobbered the "(+ (point) 1000)" part in the previous patch.
>
> Please use this one instead.

Alan, could you please take a look at the below patch?

Thanks in advance.

> From 0515de23d84a48c57b456c6730f826c5d783b965 Mon Sep 17 00:00:00 2001
> From: Olivier Certner <olce.emacs@certner.fr>
> Date: Wed, 3 May 2023 11:44:43 +0200
> Subject: [PATCH] CC Mode: Fix K&R argument declaration misdetection after
>  parenthesized type
>
> * lisp/progmodes/cc-engine.el (c-in-knr-argdecl): When trying to loop
> over candidate declarations (between the function declaration's
> identifier list's end and point) to check whether the names of their
> identifiers correspond with that of the identifier list, actually fail
> as soon as stumbling on something other than a declaration instead of
> silently succeeding, which causes some constructs to be erroneously
> recognized as K&R argument declarations.
>
> (Bug#63285)
> ---
>  lisp/progmodes/cc-engine.el | 29 ++++++++++++++++++-----------
>  1 file changed, 18 insertions(+), 11 deletions(-)
>
> diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
> index 8b34daf03c2..27740b4903c 100644
> --- a/lisp/progmodes/cc-engine.el
> +++ b/lisp/progmodes/cc-engine.el
> @@ -12039,17 +12039,24 @@ c-in-knr-argdecl
>  		       ;; Each time around the following checks one
>  		       ;; declaration (which may contain several identifiers).
>  		       (while (and
> -			       (consp (setq decl-or-cast
> -					    (c-forward-decl-or-cast-1
> -					     after-prec-token
> -					     nil ; Or 'arglist ???
> -					     nil)))
> -			       (memq (char-after) '(?\; ?\,))
> -			       (goto-char (car decl-or-cast))
> -			       (save-excursion
> -				 (setq semi-position+1
> -				       (c-syntactic-re-search-forward
> -					";" (+ (point) 1000) t)))
> +			       (or
> +				(and
> +				 (consp (setq decl-or-cast
> +					      (c-forward-decl-or-cast-1
> +					       after-prec-token
> +					       nil ; Or 'arglist ???
> +					       nil)))
> +				 (memq (char-after) '(?\; ?\,))
> +				 (goto-char (car decl-or-cast))
> +				 (save-excursion
> +				   (setq semi-position+1
> +					 (1+ (or
> +					      (c-syntactic-re-search-forward
> +					       ";" (+ (point) 1000) t)
> +					      (1- (point-max)))))))
> +				;; Can't parse declarations correctly,
> +				;; bail out.
> +				(throw 'knr nil))
>  			       (c-do-declarators
>  				semi-position+1 t nil nil
>  				(lambda (id-start id-end _next _not-top
> --
> 2.39.2





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type
  2023-10-05 20:43   ` Stefan Kangas
@ 2023-10-06  9:09     ` Alan Mackenzie
  2023-10-06 13:18     ` Alan Mackenzie
  1 sibling, 0 replies; 8+ messages in thread
From: Alan Mackenzie @ 2023-10-06  9:09 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 63285, Olivier Certner

Hello, Stefan.

On Thu, Oct 05, 2023 at 20:43:24 +0000, Stefan Kangas wrote:
> Olivier Certner <ocert.dev@free.fr> writes:

> > Sorry, I had clobbered the "(+ (point) 1000)" part in the previous patch.
> >
> > Please use this one instead.

> Alan, could you please take a look at the below patch?

I'm looking at the bug, now.  I'll get back again once I think I
understand it.  ;-)

> Thanks in advance.

[ .... ]

-- 
Alan Mackenzie (Nuremberg, Germany).





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type
  2023-10-05 20:43   ` Stefan Kangas
  2023-10-06  9:09     ` Alan Mackenzie
@ 2023-10-06 13:18     ` Alan Mackenzie
  2023-10-13 14:38       ` Alan Mackenzie
  1 sibling, 1 reply; 8+ messages in thread
From: Alan Mackenzie @ 2023-10-06 13:18 UTC (permalink / raw)
  To: Olivier Certner, Stefan Kangas; +Cc: acm, 63285

Hello, Olivier and Stefan.

On Thu, Oct 05, 2023 at 20:43:24 +0000, Stefan Kangas wrote:
> Olivier Certner <ocert.dev@free.fr> writes:

> > Sorry, I had clobbered the "(+ (point) 1000)" part in the previous patch.
> >
> > Please use this one instead.

Olivier, thanks for taking the trouble to submit this bug report.  Sorry
it's taken me so long to getting around to looking at it.

> Alan, could you please take a look at the below patch?

> Thanks in advance.

> > From 0515de23d84a48c57b456c6730f826c5d783b965 Mon Sep 17 00:00:00 2001
> > From: Olivier Certner <olce.emacs@certner.fr>
> > Date: Wed, 3 May 2023 11:44:43 +0200
> > Subject: [PATCH] CC Mode: Fix K&R argument declaration misdetection after
> >  parenthesized type
> >
> > * lisp/progmodes/cc-engine.el (c-in-knr-argdecl): When trying to loop
> > over candidate declarations (between the function declaration's
> > identifier list's end and point) to check whether the names of their
> > identifiers correspond with that of the identifier list, actually fail
> > as soon as stumbling on something other than a declaration instead of
> > silently succeeding, which causes some constructs to be erroneously
> > recognized as K&R argument declarations.

I don't think this would be quite the right way to go.  It would be too
rigorous in rejecting partially written K&R constructs which might cause
them to be excessively reindented as commas and or semicolons get typed.

It seems that (throw 'knr nil) when the c-forward-decl-or-cast-1 form
fails is sufficient to prevent the wrong recognition of your test file's
line 2 as a K&R declaration.  Please feel free to try out my (simpler)
patch below on your real C code.

Also, I propose adopting your 2-line test file, reindented, as a new
test in the CC Mode test suite.  Please let me know if you'd prefer that
not to happen.

> > (Bug#63285)
> > ---
> >  lisp/progmodes/cc-engine.el | 29 ++++++++++++++++++-----------
> >  1 file changed, 18 insertions(+), 11 deletions(-)
> >
> > diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
> > index 8b34daf03c2..27740b4903c 100644
> > --- a/lisp/progmodes/cc-engine.el
> > +++ b/lisp/progmodes/cc-engine.el
> > @@ -12039,17 +12039,24 @@ c-in-knr-argdecl
> >  		       ;; Each time around the following checks one
> >  		       ;; declaration (which may contain several identifiers).
> >  		       (while (and
> > -			       (consp (setq decl-or-cast
> > -					    (c-forward-decl-or-cast-1
> > -					     after-prec-token
> > -					     nil ; Or 'arglist ???
> > -					     nil)))
> > -			       (memq (char-after) '(?\; ?\,))
> > -			       (goto-char (car decl-or-cast))
> > -			       (save-excursion
> > -				 (setq semi-position+1
> > -				       (c-syntactic-re-search-forward
> > -					";" (+ (point) 1000) t)))
> > +			       (or
> > +				(and
> > +				 (consp (setq decl-or-cast
> > +					      (c-forward-decl-or-cast-1
> > +					       after-prec-token
> > +					       nil ; Or 'arglist ???
> > +					       nil)))
> > +				 (memq (char-after) '(?\; ?\,))
> > +				 (goto-char (car decl-or-cast))
> > +				 (save-excursion
> > +				   (setq semi-position+1
> > +					 (1+ (or
> > +					      (c-syntactic-re-search-forward
> > +					       ";" (+ (point) 1000) t)
> > +					      (1- (point-max)))))))
> > +				;; Can't parse declarations correctly,
> > +				;; bail out.
> > +				(throw 'knr nil))
> >  			       (c-do-declarators
> >  				semi-position+1 t nil nil
> >  				(lambda (id-start id-end _next _not-top
> > --
> > 2.39.2

Here's my amended patch:



diff -r b680bbba3141 cc-engine.el
--- a/cc-engine.el	Fri Sep 29 11:15:58 2023 +0000
+++ b/cc-engine.el	Fri Oct 06 11:22:31 2023 +0000
@@ -12285,11 +12285,14 @@
 		       ;; Each time around the following checks one
 		       ;; declaration (which may contain several identifiers).
 		       (while (and
-			       (consp (setq decl-or-cast
-					    (c-forward-decl-or-cast-1
-					     after-prec-token
-					     nil ; Or 'arglist ???
-					     nil)))
+			       (not (eq (char-after) ?{))
+			       (or
+				(consp (setq decl-or-cast
+					     (c-forward-decl-or-cast-1
+					      after-prec-token
+					      nil ; Or 'arglist ???
+					      nil)))
+				(throw 'knr nil))
 			       (memq (char-after) '(?\; ?\,))
 			       (goto-char (car decl-or-cast))
 			       (save-excursion


-- 
Alan Mackenzie (Nuremberg, Germany).





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type
  2023-10-06 13:18     ` Alan Mackenzie
@ 2023-10-13 14:38       ` Alan Mackenzie
  2023-10-13 15:04         ` Stefan Kangas
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Mackenzie @ 2023-10-13 14:38 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: acm, 63285-done

Hello, Stefan.

I've committed a patch which fixes this bug.  I'm now closing it.

-- 
Alan Mackenzie (Nuremberg, Germany).





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type
  2023-10-13 14:38       ` Alan Mackenzie
@ 2023-10-13 15:04         ` Stefan Kangas
  0 siblings, 0 replies; 8+ messages in thread
From: Stefan Kangas @ 2023-10-13 15:04 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: 63285-done

Alan Mackenzie <acm@muc.de> writes:

> I've committed a patch which fixes this bug.  I'm now closing it.

Thanks!





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-10-13 15:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-04 21:54 bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type Olivier Certner
     [not found] ` <handler.63285.B.168323729911280.ack@debbugs.gnu.org>
2023-05-04 21:57   ` bug#63285: Acknowledgement (30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type) Olivier Certner
2023-05-04 22:14 ` bug#63285: 30.0.50; CC Mode: K&R argument declaration misdetection after parenthesized type Olivier Certner
2023-10-05 20:43   ` Stefan Kangas
2023-10-06  9:09     ` Alan Mackenzie
2023-10-06 13:18     ` Alan Mackenzie
2023-10-13 14:38       ` Alan Mackenzie
2023-10-13 15:04         ` Stefan Kangas

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).