Subject: bug#45160: 27.1; [C] wrong indent for knr-style arguments declaration

From: Alan Mackenzie <[email-masked]>
Date: Fri, 11 Dec 2020 16:33:28 +0000

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).