Subject: Re: bug#33649: 26.1.50; variable is highlighted as type in c++ mode

From: Alan Mackenzie <[email-masked]>
Date: 7 Dec 2018 16:39:51 -0000

Hello Shanavas.

In article <[email-masked]> you wrote:
> [-- text/plain, encoding quoted-printable, charset: utf-8, 15 lines --]

> For the following snippet,

> ```
> int a = 9;
> func(a);
> func(a * 9);
> ```
> `a` is highlighted differently in two function calls.
> In `func(a)` it is highlighted as a variable where in `func(a * 9)` it is highlighted as a type.

Yes.  It is due to CC Mode parsing "a *" as a "pointer to type a"
without taking into account the following "9".  Thanks for taking the
trouble to report this bug.

The following patch should fix this.  Would you please try it out on
your real source code, and either confirm to me that it works, or tell
me what is still wrong.

The patch should be applied to emacs/lisp/progmodes/cc-engine.el, and
you should then byte-compile the patched file.  If you want any help
with the patching or the byte compilation, feel free to send me private
mail.

Thanks again, and looking forward to hearing back from you.

diff -r 021672422937 cc-engine.el
--- a/cc-engine.el	Sat Nov 24 10:18:12 2018 +0000
+++ b/cc-engine.el	Fri Dec 07 16:23:37 2018 +0000
@@ -8551,6 +8551,8 @@
 	  got-parens
 	  ;; True if there is an identifier in the declarator.
 	  got-identifier
+	  ;; True if we find a number where an identifier was expected.
+	  got-number
 	  ;; True if there's a non-close-paren match of
 	  ;; `c-type-decl-suffix-key'.
 	  got-suffix
@@ -8628,7 +8630,9 @@
 	  (and (looking-at c-identifier-start)
 	       (setq pos (point))
 	       (setq got-identifier (c-forward-name))
-	       (setq name-start pos)))
+	       (setq name-start pos))
+	  (when (looking-at "[0-9]")
+	    (setq got-number t))) ; We've probably got an arithmetic expression.
 
       ;; Skip over type decl suffix operators and trailing noise macros.
       (while
@@ -9102,7 +9106,7 @@
 
 	   ;; CASE 18
 	   (when (and (not (memq context '(nil top)))
-		      (or got-prefix
+		      (or (and got-prefix (not got-number))
 			  (and (eq context 'decl)
 			       (not c-recognize-paren-inits)
 			       (or got-parens got-suffix))))

> Major mode: C++//l
> -- 
> ഷാനവാസ്
> Sent from my Android device with K-9 Mail.

-- 
Alan Mackenzie (Nuremberg, Germany).