unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Alan Mackenzie <acm@muc.de>
To: Bruno <braoult@gmail.com>
Cc: acm@muc.de, 45160@debbugs.gnu.org
Subject: bug#45160: 27.1; [C] wrong indent for knr-style arguments declaration
Date: Fri, 11 Dec 2020 16:33:28 +0000	[thread overview]
Message-ID: <X9OfWH7qzSNa4UYC@ACM> (raw)
In-Reply-To: <87h7otr9g5.fsf@lorien.i-did-not-set--mail-host-address--so-tickle-me>

Hello, Bruno.

Thank you indeed for taking the trouble to report this bugi, and thanks
even more for cutting the problem down to a minimal test case.

On Thu, Dec 10, 2020 at 16:02:02 +0100, Bruno wrote:
> Within "emacs -Q" session, open the following C source file:

> ------------------------------ foo.c
> int f(i, j, k)
> int i;
> int j;
> int k;
> {
> return 1;
> }

> int g(i, j, k)
> int i;
> int j, k;
> {
> return 1;
> }
> ------------------------------

> Then, do "C-c ." (c-set-style) to "k&r", and "c-c c-o" (c-set-offset),
> and set "knr-argdecl-intro" to "+".

> We have now the following values in c-offsets-alist :
> knr-argdecl-intro : +  (First line of a K&R C argument declaration)
> knr-argdecl : 0        (Subsequent lines in a K&R C argument declaration)

> re-indent foo.c. At this stage we expect the following indentation for
> arguments declarations.

> ------------------------------ expected indentation
> int f(i, j, k)
>     int i;
>     int j;
>     int k;
> {
>     return 1;
> }

> int g(i, j, k)
>     int i;
>     int j, k;
> {
>     return 1;
> }
> ------------------------------

> But we get the following result (please note line 11).

> ------------------------------ emacs indentation
> int f(i, j, k)
>     int i;
>     int j;
>     int k;
> {
>     return 1;
> }

> int g(i, j, k)
>     int i;
> int j, k;
> {
>     return 1;
> }
> ------------------------------

> It should be noted that if we separate each argument on separate lines
> (function f), indentation is correct.
> It is incorrect if multiple variables are in same declaration (function
> g).

Yes.  Somehow, CC Mode managed not to cater for the occurrence of two or
more identifiers in the same declaration.

> Also, multiple variables on first arguments declaration line seems to
> have correct indentation.

Indeed.

The following patch should fix the problem.  Would you please apply it
to your Emacs (cc-engine.el is in .../emacs/lisp/progmodes) and byte
compile cc-engine.el.  Then please try it out on your real source code,
and either confirm the bug has indeed been fixed, or say what's still
wrong.  (If you would like any help with the patching or byte compiling,
feel free to send me private email.)



diff -r 863d08a1858a cc-engine.el
--- a/cc-engine.el	Thu Nov 26 11:27:52 2020 +0000
+++ b/cc-engine.el	Fri Dec 11 15:57:08 2020 +0000
@@ -10849,11 +10849,11 @@
 	     (low-lim (max (or lim (point-min))   (or macro-start (point-min))))
 	     before-lparen after-rparen
 	     (here (point))
-	     (pp-count-out 20)	; Max number of paren/brace constructs before
-				; we give up.
+	     (pp-count-out 20)	 ; Max number of paren/brace constructs before
+					; we give up
 	     ids	      ; List of identifiers in the parenthesized list.
 	     id-start after-prec-token decl-or-cast decl-res
-	     c-last-identifier-range identifier-ok)
+	     c-last-identifier-range semi-position+1)
 	(narrow-to-region low-lim (or macro-end (point-max)))
 
 	;; Search backwards for the defun's argument list.  We give up if we
@@ -10887,8 +10887,8 @@
 		   (setq after-rparen (point)))
 		  ((eq (char-before) ?\])
 		   (setq after-rparen nil))
-		  (t ; either } (hit previous defun) or = or no more
-		     ; parens/brackets.
+		  (t	       ; either } (hit previous defun) or = or no more
+					; parens/brackets.
 		   (throw 'knr nil)))
 
 	    (if after-rparen
@@ -10945,31 +10945,35 @@
 		       (forward-char)	; over the )
 		       (setq after-prec-token after-rparen)
 		       (c-forward-syntactic-ws)
+		       ;; Each time around the following checks one
+		       ;; declaration (which may contain several identifiers).
 		       (while (and
-			       (or (consp (setq decl-or-cast
-						(c-forward-decl-or-cast-1
-						 after-prec-token
-						 nil ; Or 'arglist ???
-						 nil)))
-				   (progn
-				     (goto-char after-prec-token)
-				     (c-forward-syntactic-ws)
-				     (setq identifier-ok (eq (char-after) ?{))
-				     nil))
-			       (eq (char-after) ?\;)
-			       (setq after-prec-token (1+ (point)))
+			       (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))
-			       (setq decl-res (c-forward-declarator))
-			       (setq identifier-ok
-				     (member (buffer-substring-no-properties
-					(car decl-res) (cadr decl-res))
-				       ids))
-			       (progn
-				 (goto-char after-prec-token)
-				 (prog1 (< (point) here)
-				   (c-forward-syntactic-ws))))
-			 (setq identifier-ok nil))
-		       identifier-ok))
+			       (save-excursion
+				 (setq semi-position+1
+				       (c-syntactic-re-search-forward
+					";" (+ (point) 1000) t)))
+			       (c-do-declarators
+				semi-position+1 t nil nil
+				(lambda (id-start id-end _next _not-top
+						  _func _init)
+				  (if (not (member
+					    (buffer-substring-no-properties
+					     id-start id-end)
+					    ids))
+				      (throw 'knr nil))))
+
+			       (progn (forward-char)
+				      (<= (point) here))
+			       (progn (c-forward-syntactic-ws)
+				      t)))
+		       t))
 		    ;; ...Yes.  We've identified the function's argument list.
 		    (throw 'knr
 			   (progn (goto-char after-rparen)



> Regards,

> Bruno.

> In GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 2.24.32)
>  of 2020-10-29 built on lorien
> Windowing system distributor 'The X.Org Foundation', version 11.0.12008000
> System Description: Ubuntu 20.04.1 LTS

-- 
Alan Mackenzie (Nuremberg, Germany).





  reply	other threads:[~2020-12-11 16:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-10 15:02 bug#45160: 27.1; [C] wrong indent for knr-style arguments declaration Bruno
2020-12-11 16:33 ` Alan Mackenzie [this message]
2020-12-11 19:22   ` Bruno Raoult
2020-12-12 14:48     ` Alan Mackenzie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=X9OfWH7qzSNa4UYC@ACM \
    --to=acm@muc.de \
    --cc=45160@debbugs.gnu.org \
    --cc=braoult@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).