Subject: bug#7579: cc-mode c++ template-typed variable decl regression

From: Alan Mackenzie <[email-masked]>
Date: Tue, 21 Feb 2012 14:05:26 +0000

Hello, Daniel.

On Tue, Feb 14, 2012 at 09:16:25AM -0800, Daniel Colascione wrote:
> Hi Alan,

> On 2/14/12 8:11 AM, Alan Mackenzie wrote:
> >>>> /* In Emacs 23, adffdfa below is fontified as a variable. In Emacs HEAD,
> >>>> it is not. */

> >>>> void foo()
> >>>> {
> >>>>     mumble x(5);
> >>>>     std::vector<int> adffdfa(1,2,3);
> >>>> };

> >> Are there any syntactic clues here that a variable rather than a function
> >> is being declared?  All I can see is that numeric literals take the place
> >> of "parameters".

> > Please ignore this request.  Obviously, for a function, there must be
> > either nothing in the parentheses or  [<type> <value>]*.

> Or just types:

> std::vector <int> blah(int);

> typedef int foo;
> std::vector <int> blah(foo);

> Still, unless we're absolutely sure we're looking at a function
> declaration, we should fontify a declaration as a variables
> declaration. Function-scope function declarations are extremely
> uncommon, and at least in my experience, almost always offset by
> keywords like "extern". Even if something like int bar(); could be a
> function, fontifying it as a variable would be the right thing to do
> in function scope for C++.

OK, here's a patch to try out.  As you requested, everything which might
look like a function which is directly within a function is treated as a
variable declaration, with one exception.  That is a construct like

    int (*foo) (bar);

, where foo continues to be a function.

If you can think of any other constructs which should still be functions,
please let me know.  Please give the patch a stress test and let me know
how it works.

diff -r 9e9f3f646393 cc-fonts.el
--- a/cc-fonts.el	Sun Feb 19 18:19:16 2012 +0000
+++ b/cc-fonts.el	Tue Feb 21 13:58:37 2012 +0000
@@ -1009,6 +1009,7 @@
        paren-depth
        id-face got-init
        c-last-identifier-range
+       maybe-function-type
        (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
 
     ;; The following `while' fontifies a single declarator id each time round.
@@ -1054,7 +1055,10 @@
 		(if (eq (char-after) ?\()
 		    (progn
 		      (setq paren-depth (1+ paren-depth))
-		      (forward-char))
+		      (forward-char)
+		      (c-forward-syntactic-ws)
+		      (setq maybe-function-type
+			    (eq (char-after) ?*)))
 		  (goto-char (match-end 1)))
 		(c-forward-syntactic-ws))
 
@@ -1088,7 +1092,15 @@
 	     "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
 
       (setq next-pos (match-beginning 0)
-	    id-face (if (eq (char-after next-pos) ?\()
+	    id-face (if (and (eq (char-after next-pos) ?\()
+			     (or maybe-function-type
+				 (not (and (c-major-mode-is 'c++-mode)
+					   (let (c-last-identifier-range
+						 c-record-type-identifiers)
+					     (save-excursion
+					       (goto-char id-start)
+					       (not (c-at-toplevel-p))))))))
+				 
 			'font-lock-function-name-face
 		      'font-lock-variable-name-face)
 	    got-init (and (match-beginning 1)
@@ -1141,6 +1153,7 @@
 
 	;; If a ',' is found we set pos to the next declarator and iterate.
 	(when (and (< (point) limit) (looking-at ","))
+	  (setq maybe-function-type nil)
 	  (c-put-char-property (point) 'c-type separator-prop)
 	  (forward-char)
 	  (c-forward-syntactic-ws limit)

-- 
Alan Mackenzie (Nuremberg, Germany).