Subject: bug#23818: 25.0.95.3: c-beginning-of-defun misbehaviour

From: Alan Mackenzie <[email-masked]>
Date: 24 Jun 2016 15:02:34 -0000

Hello, Rolf

In article <[email-masked]> you wrote:

> The same in 24.5 and 25.0.95.3:

> emacs -Q

> Open some random emtpy buffer foo.c, put it in c-mode (M-x c-mode) and
> insert the following C code:

> #define DBG(x) x

> DBG(
> static void __dbgAttr () {
>     /* something */
> }
> )

> int main (void) 
> {
>     int i;
>     i++;
>     i++;
>     return i;
> }

> int foo () 
> {
>     int i;
>     i++;
>     i++;
>     return 1;
> }

> Put the point inside function main and C-M-home (or M-x
> c-beginning-of-defun). Instead of the beginning of main() the point is
> here:

> _P_DBG(
> ...

> Far away from

> _P_int main(void)
> ...

Thank you for the bug report, and thanks even more for making it a nice
concise easy to work with example.

> This isn't "unbalanced braces in preprocessor statements are
> horrendously difficult to parse" as in bug #23775, there are no
> unbalanced braces everywhere. It's that some code above the code of a
> syntactical correct function disturbs c-beginning-of-defun in finding
> the beginning of the function.

There are two things here.  The first is that you must configure "DBG" as
a "macro with a semicolon", as detailed in the CC Mode manual, page
"Macros with ;".  For example, you could put the following into your
c-mode-common-hook:

    (setq c-macro-names-with-semicolon '("DBG"))
    (c-make-macro-with-semi-re)

.  That would set up that macro for all your C files.  c-mode-common-hook
is more precisely described on pages "Configuration Basics" and "CC
Hooks" in the CC Mode manual.

The second part of the fix is an actual bug where the software fails to
check for "macros with semicolons" at a critical point.  For that, could
you install the following patch, please, then byte-compile cc-engine.el:

diff -r 4c8ccaedfd6a cc-engine.el
--- a/cc-engine.el	Fri Jun 24 13:06:30 2016 +0000
+++ b/cc-engine.el	Fri Jun 24 14:55:30 2016 +0000
@@ -9135,7 +9135,8 @@
 		(/= last-stmt-start (point))
 		(progn
 		  (c-backward-syntactic-ws lim)
-		  (not (memq (char-before) '(?\; ?} ?: nil))))
+		  (not (or (memq (char-before) '(?\; ?} ?: nil))
+			   (c-at-vsemi-p))))
 		(save-excursion
 		  (backward-char)
 		  (not (looking-at "\\s(")))

If you want any help with applying the patch, or byte compiling, or
setting up a hook, etc., feel free to send me a private email.

[ .... ]

When you've done all this, could you please confirm that it fixes the
problem so I can close the bug, or tell me what's still buggy.

Thanks!

-- 
Alan Mackenzie (Nuremberg, Germany).