From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#24377: 25.1; CC-Mode (C++) uses font-lock-type-face for ordinary variable Date: 13 Sep 2016 18:35:14 -0000 Organization: muc.de e.V. Message-ID: <20160913183514.30625.qmail@mail.muc.de> References: <87lgz5l1ly.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1473791794 319 195.159.176.226 (13 Sep 2016 18:36:34 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 13 Sep 2016 18:36:34 +0000 (UTC) User-Agent: tin/2.3.1-20141224 ("Tallant") (UNIX) (FreeBSD/10.3-RELEASE-p7 (amd64)) Cc: 24377@debbugs.gnu.org To: Bastian Beischer Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Sep 13 20:36:30 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bjsZC-0007Ea-GA for geb-bug-gnu-emacs@m.gmane.org; Tue, 13 Sep 2016 20:36:22 +0200 Original-Received: from localhost ([::1]:50947 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjsZD-0008Uf-IH for geb-bug-gnu-emacs@m.gmane.org; Tue, 13 Sep 2016 14:36:23 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55856) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjsYv-0008Ko-Sk for bug-gnu-emacs@gnu.org; Tue, 13 Sep 2016 14:36:10 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bjsYs-0002lb-29 for bug-gnu-emacs@gnu.org; Tue, 13 Sep 2016 14:36:04 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:32947) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjsYr-0002lT-Uj; Tue, 13 Sep 2016 14:36:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1bjsYr-00006o-Nh; Tue, 13 Sep 2016 14:36:01 -0400 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: Tue, 13 Sep 2016 18:36:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 24377 X-GNU-PR-Package: emacs,cc-mode X-GNU-PR-Keywords: Original-Received: via spool by 24377-submit@debbugs.gnu.org id=B24377.1473791719365 (code B ref 24377); Tue, 13 Sep 2016 18:36:01 +0000 Original-Received: (at 24377) by debbugs.gnu.org; 13 Sep 2016 18:35:19 +0000 Original-Received: from localhost ([127.0.0.1]:58892 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bjsYB-00005o-Cd for submit@debbugs.gnu.org; Tue, 13 Sep 2016 14:35:19 -0400 Original-Received: from mail.muc.de ([193.149.48.3]:33489) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bjsY9-00005d-2s for 24377@debbugs.gnu.org; Tue, 13 Sep 2016 14:35:18 -0400 Original-Received: (qmail 30626 invoked by uid 3782); 13 Sep 2016 18:35:14 -0000 In-Reply-To: X-Newsgroups: gnu.emacs.bug 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" Xref: news.gmane.org gmane.emacs.bugs:123267 Archived-At: Hello, Bastian. In article you wrote: > This piece of C++ code is wrongly fontified by CC-Mode: > int b = 5; > int a = (b*3); > Reproduce as follows: > 1) emacs -Q > 2) Open file "test.C" > 3) Paste snippet in buffer > 4) Place cursor over "b" in second line > 5) M-x describe-face -> font-lock-type-face > This appears to be correlated with the presence of the opening > parentheses and the '*' character, because: > 1) int a = b*3; > does not have this problem > 2) "b" gets fontified incorrectly after entering the '*' character: > int a = (b* Thanks for this bug report, and thanks even more for taking the trouble to reduce the test case to just two lines. > In GNU Emacs 25.1.2 (x86_64-unknown-linux-gnu, GTK+ Version 2.24.30) > of 2016-08-30 built on beischer-w520 > Repository revision: 17197d0ae9eac100b56078b2a48964b29710b76e > Windowing system distributor 'The X.Org Foundation', version 11.0.11804000 > Configured using: > 'configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib > --localstatedir=/var --mandir=/usr/share/man --with-gameuser=:games > --with-sound=alsa --with-xft --with-modules --with-x-toolkit=gtk2 > --with-gconf --without-gsettings 'CFLAGS=-march=native -O2 -pipe > -fstack-protector-strong' CPPFLAGS=-D_FORTIFY_SOURCE=2 > LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro' [ .... ] Yes, the CC Mode fontification code was confusing the "*" with an indirection operator, such as you might get in "int (*b) (....);" The solution seems to be to recognise the case where an arithmetic operator (including "=") precedes the open parenthesis. This is what the following patch does. Would you please try out this patch on real code, and let me know whether the problem is completely fixed, and if not, what's still going wrong. Thanks! After applying the patch, plese recompile all of CC Mode, since the change is partly in Lisp macros, which need to propagate to other files. Here's the patch: diff -r 9ff65a2d07bd cc-fonts.el --- a/cc-fonts.el Sun Sep 11 20:46:35 2016 +0000 +++ b/cc-fonts.el Tue Sep 13 18:22:17 2016 +0000 @@ -1310,6 +1310,13 @@ ;; multiline declaration. (c-put-char-property (1- match-pos) 'c-type 'c-decl-arg-start)) + ;; Got an open paren preceded by an arith operator. + ((and (eq (char-before match-pos) ?\() + (save-excursion + (and (zerop (c-backward-token-2 2)) + (looking-at c-arithmetic-op-regexp)))) + (setq context nil + c-restricted-<>-arglists nil)) (t (setq context 'arglist c-restricted-<>-arglists t)))) diff -r 9ff65a2d07bd cc-langs.el --- a/cc-langs.el Sun Sep 11 20:46:35 2016 +0000 +++ b/cc-langs.el Tue Sep 13 18:22:17 2016 +0000 @@ -1231,6 +1231,22 @@ (c-lang-defvar c-assignment-op-regexp (c-lang-const c-assignment-op-regexp)) +(c-lang-defconst c-arithmetic-operators + "List of all arithmetic operators, including \"+=\", etc." + ;; Note: in the following, there are too many operators for AWK and IDL. + t (append (c-lang-const c-assignment-operators) + '("+" "-" "*" "/" "%" + "<<" ">>" + "<" ">" "<=" ">=" + "==" "!=" + "&" "^" "|" + "&&" "||"))) + +(c-lang-defconst c-arithmetic-op-regexp + t (c-make-keywords-re nil + (c-lang-const c-arithmetic-operators))) +(c-lang-defvar c-arithmetic-op-regexp (c-lang-const c-arithmetic-op-regexp)) + (c-lang-defconst c-:$-multichar-token-regexp ;; Regexp matching all tokens ending in ":" which are longer than one char. ;; Currently (2016-01-07) only used in C++ Mode. -- Alan Mackenzie (Nuremberg, Germany).