Subject: bug#63328: CC Mode 5.35.2 (Java//l); Constructor function not fontified as such

From: Alan Mackenzie <[email-masked]>
Date: Thu, 11 May 2023 17:42:12 +0000

Hello, Po.

Thanks for yet another bug report!

On Sat, May 06, 2023 at 19:23:27 +0800, Po Lu via CC-Mode-help wrote:
> Package: cc-mode

> In the following code:

>   public
>   EmacsInputConnection (EmacsView view)
>   {
>     super (view, true);

> `EmacsInputConnection', a constructor function, is not fontified as
> such.  Its parameter list is not fontified either.

I actually put a class around these lines for testing, as follows:

    class EmacsInputConnection {
        public
            EmacsInputConnection (EmacsView view)
        {
            super (view, true);
        }
    }

..  I don't think Java classes can have constructors anywhere but inside
the class of the same name (please correct me if I'm wrong, here).

To fix this reuired some tweaking of the low level syntactic analysis
code.  Could I ask you, please, to test the enclosed patch (which should
apply cleanly to either master or the emacs-29 branch).  Then please let
me know how the test went.

Thanks!

> Emacs  : GNU Emacs 29.0.90 (build 1, x86_64-pc-linux-gnu)
>  of 2023-04-29
> Package: CC Mode 5.35.2 (Java//l)
> Buffer Style: GNU
> c-emacs-features: (pps-extended-state col-0-paren posix-char-classes gen-string-delim gen-comment-delim syntax-properties category-properties 1-bit)

[ .... ]

-- 
Alan Mackenzie (Nuremberg, Germany).

diff -r 3d663572d4bf cc-engine.el
--- a/cc-engine.el	Wed May 03 09:45:37 2023 +0000
+++ b/cc-engine.el	Thu May 11 16:11:36 2023 +0000
@@ -10880,9 +10880,16 @@
 		     (not (memq context '(arglist decl))))
 		 (or (and new-style-auto
 			  (looking-at c-auto-ops-re))
-		     (and (or maybe-typeless backup-maybe-typeless)
-			  (not got-prefix)
-			  at-type)))
+		     (and (not got-prefix)
+			  at-type
+			  (or maybe-typeless backup-maybe-typeless
+			      ;; Do we have a (typeless) constructor?
+			      (save-excursion
+				(goto-char type-start)
+				(and
+				 (looking-at c-identifier-key)
+				 (c-directly-in-class-called-p
+				  (match-string 0))))))))
 	;; Have found no identifier but `c-typeless-decl-kwds' has
 	;; matched so we know we're inside a declaration.  The
 	;; preceding type must be the identifier instead.
@@ -12561,7 +12568,8 @@
 		   (looking-at c-class-key))
 	  (goto-char (match-end 1))
 	  (c-forward-syntactic-ws)
-	  (looking-at name))))))
+	  (and (looking-at c-identifier-key)
+	       (string= (match-string 0) name)))))))
 
 (defun c-search-uplist-for-classkey (paren-state)
   ;; Check if the closest containing paren sexp is a declaration