From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Lars Ingebrigtsen Newsgroups: gmane.emacs.bugs Subject: bug#7918: [PATCH] cc-mode: only the first clause of a for-loop should be checked for declarations Date: Fri, 26 Feb 2016 16:48:13 +1030 Message-ID: <87k2lsuh96.fsf@gnus.org> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1456467565 2942 80.91.229.3 (26 Feb 2016 06:19:25 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 26 Feb 2016 06:19:25 +0000 (UTC) Cc: 7918@debbugs.gnu.org To: Daniel Colascione Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Feb 26 07:19:14 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aZBk9-0001Fc-LZ for geb-bug-gnu-emacs@m.gmane.org; Fri, 26 Feb 2016 07:19:13 +0100 Original-Received: from localhost ([::1]:47586 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aZBk8-00072M-Vo for geb-bug-gnu-emacs@m.gmane.org; Fri, 26 Feb 2016 01:19:12 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40388) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aZBk5-0006zd-76 for bug-gnu-emacs@gnu.org; Fri, 26 Feb 2016 01:19:10 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aZBk3-0002Dm-TD for bug-gnu-emacs@gnu.org; Fri, 26 Feb 2016 01:19:09 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:49512) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aZBjz-0002Cn-9n; Fri, 26 Feb 2016 01:19:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84) (envelope-from ) id 1aZBjz-0008Dv-6D; Fri, 26 Feb 2016 01:19:03 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Lars Ingebrigtsen Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org, bug-cc-mode@gnu.org Resent-Date: Fri, 26 Feb 2016 06:19:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 7918 X-GNU-PR-Package: emacs,cc-mode X-GNU-PR-Keywords: patch X-Debbugs-Original-Cc: bug-cc-mode@gnu.org, 7918@debbugs.gnu.org Original-Received: via spool by 7918-submit@debbugs.gnu.org id=B7918.145646753031553 (code B ref 7918); Fri, 26 Feb 2016 06:19:03 +0000 Original-Received: (at 7918) by debbugs.gnu.org; 26 Feb 2016 06:18:50 +0000 Original-Received: from localhost ([127.0.0.1]:46630 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84) (envelope-from ) id 1aZBjm-0008Cq-2A for submit@debbugs.gnu.org; Fri, 26 Feb 2016 01:18:50 -0500 Original-Received: from hermes.netfonds.no ([80.91.224.195]:33535) by debbugs.gnu.org with esmtp (Exim 4.84) (envelope-from ) id 1aZBjk-0008Cj-8y for 7918@debbugs.gnu.org; Fri, 26 Feb 2016 01:18:48 -0500 Original-Received: from [175.103.25.178] (helo=mouse) by hermes.netfonds.no with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1aZBjH-00058A-Re; Fri, 26 Feb 2016 07:18:20 +0100 In-Reply-To: (Daniel Colascione's message of "Tue, 25 Jan 2011 22:36:22 -0800") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux) X-MailScanner-ID: 1aZBjH-00058A-Re MailScanner-NULL-Check: 1457072300.66328@TA3pAplEU8bdgV+YarT0AA X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 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.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:113838 Archived-At: Daniel Colascione writes: > // This code has no variable declarations > > void foo() { > for (; (DWORD) a * b ;) > ; > > for (; a * b ;) > ; > } > I can confirm that the Emacs trunk still highlights the "a" in these examples wrong, and that Daniel's patch seems to fix the issue. However, I'm totally unfamiliar with the cc-mode code, so it would be nice if somebody could look at it before it's applied. === modified file 'lisp/progmodes/cc-fonts.el' --- lisp/progmodes/cc-fonts.el 2010-12-07 12:15:28 +0000 +++ lisp/progmodes/cc-fonts.el 2011-01-25 11:10:00 +0000 @@ -1080,7 +1080,8 @@ ;; o - '<> if the arglist is of angle bracket type; ;; o - 'arglist if it's some other arglist; ;; o - nil, if not in an arglist at all. This includes the - ;; parenthesised condition which follows "if", "while", etc. + ;; parenthesised condition which follows "if", "while", etc., + ;; but not "for", which is 'arglist after `;'. context ;; The position of the next token after the closing paren of ;; the last detected cast. @@ -1109,7 +1110,7 @@ ;; `c-forward-decl-or-cast-1' and `c-forward-label' for ;; later fontification. (c-record-type-identifiers t) - label-type + label-type paren-state most-enclosing-brace c-record-ref-identifiers ;; Make `c-forward-type' calls mark up template arglists if ;; it finds any. That's necessary so that we later will @@ -1171,7 +1172,6 @@ 'font-lock-function-name-face)))) (c-font-lock-function-postfix limit)) - (setq start-pos (point)) (when ;; The result of the `if' condition below is true when we don't recognize a @@ -1189,7 +1189,31 @@ ;; (e.g. "for ("). (let ((type (and (> match-pos (point-min)) (c-get-char-property (1- match-pos) 'c-type)))) - (cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?<))) + (cond + (;; Try to not fontify the second and third clauses of + ;; `for' statements as declarations. + (and (or (eq (char-before match-pos) ?\;) + (save-excursion + ;; Catch things like for(; (DWORD)(int) x & + ;; y; ) without invoking the full might of + ;; c-beginning-of-statement-1. + (goto-char match-pos) + (while (eq (char-before) ?\)) + (c-go-list-backward) + (c-backward-syntactic-ws)) + (eq (char-before) ?\;))) + + (setq paren-state (c-parse-state)) + (setq most-enclosing-brace + (c-most-enclosing-brace paren-state)) + (eq (char-after most-enclosing-brace) ?\()) + + ;; After a ";" in a for-block. A declaration can never + ;; begin after a `;' if the most enclosing paren is a + ;; `('. + (setq context 'arglist + c-restricted-<>-arglists t)) + ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?<))) (setq context nil c-restricted-<>-arglists nil)) ;; A control flow expression @@ -1252,7 +1276,7 @@ ;; Are we at a declarator? Try to go back to the declaration ;; to check this. Note that `c-beginning-of-decl-1' is slow, ;; so we cache its result between calls. - (let (paren-state bod-res encl-pos is-typedef) + (let (bod-res encl-pos is-typedef) (goto-char start-pos) (save-excursion (unless (and decl-search-lim @@ -1318,20 +1342,7 @@ ;; Back up to the type to fontify the declarator(s). (goto-char (car decl-or-cast)) - (let ((decl-list - (if context - ;; Should normally not fontify a list of - ;; declarators inside an arglist, but the first - ;; argument in the ';' separated list of a "for" - ;; statement is an exception. - (when (eq (char-before match-pos) ?\() - (save-excursion - (goto-char (1- match-pos)) - (c-backward-syntactic-ws) - (and (c-simple-skip-symbol-backward) - (looking-at c-paren-stmt-key)))) - t))) - + (let ((decl-list (not context))) ;; Fix the `c-decl-id-start' or `c-decl-type-start' property ;; before the first declarator if it's a list. ;; `c-font-lock-declarators' handles the rest. -- (domestic pets only, the antidote for overdose, milk.) bloggy blog: http://lars.ingebrigtsen.no