From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#45160: 27.1; [C] wrong indent for knr-style arguments declaration Date: Fri, 11 Dec 2020 16:33:28 +0000 Message-ID: References: <87h7otr9g5.fsf@lorien.i-did-not-set--mail-host-address--so-tickle-me> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15959"; mail-complaints-to="usenet@ciao.gmane.io" Cc: acm@muc.de, 45160@debbugs.gnu.org To: Bruno Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Dec 11 17:34:37 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1knlNN-00043B-Ie for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 11 Dec 2020 17:34:37 +0100 Original-Received: from localhost ([::1]:36876 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1knlNM-0006DS-Ji for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 11 Dec 2020 11:34:36 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41482) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1knlMq-0006Cw-TG for bug-gnu-emacs@gnu.org; Fri, 11 Dec 2020 11:34:06 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:59443) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1knlMo-0005ew-1w; Fri, 11 Dec 2020 11:34:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1knlMn-0003dq-T3; Fri, 11 Dec 2020 11:34:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org, bug-cc-mode@gnu.org Resent-Date: Fri, 11 Dec 2020 16:34:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45160 X-GNU-PR-Package: emacs,cc-mode Original-Received: via spool by 45160-submit@debbugs.gnu.org id=B45160.160770441813959 (code B ref 45160); Fri, 11 Dec 2020 16:34:01 +0000 Original-Received: (at 45160) by debbugs.gnu.org; 11 Dec 2020 16:33:38 +0000 Original-Received: from localhost ([127.0.0.1]:42756 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1knlMP-0003d5-LC for submit@debbugs.gnu.org; Fri, 11 Dec 2020 11:33:37 -0500 Original-Received: from colin.muc.de ([193.149.48.1]:30586 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1knlMN-0003cs-PB for 45160@debbugs.gnu.org; Fri, 11 Dec 2020 11:33:36 -0500 Original-Received: (qmail 77326 invoked by uid 3782); 11 Dec 2020 16:33:28 -0000 Original-Received: from acm.muc.de (p4fe15d97.dip0.t-ipconnect.de [79.225.93.151]) by localhost.muc.de (tmda-ofmipd) with ESMTP; Fri, 11 Dec 2020 17:33:28 +0100 Original-Received: (qmail 8926 invoked by uid 1000); 11 Dec 2020 16:33:28 -0000 Content-Disposition: inline In-Reply-To: <87h7otr9g5.fsf@lorien.i-did-not-set--mail-host-address--so-tickle-me> X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:195785 Archived-At: 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).