all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#14133: 24.2; c functions recognition breaks on certain preprocessor macros
@ 2013-04-03 10:48 Gauthier Ostervall
  2013-04-09  6:33 ` Gauthier Östervall
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Gauthier Ostervall @ 2013-04-03 10:48 UTC (permalink / raw)
  To: 14133

Function coloring, c-beginning-of-defun and c-end-of-defun behaves
strangely if the functions contain ifdef'd code.

For example:
void function_one(void)
{
    // Make the number of spaces before # vary.
    #if defined(DEBUG)
    trap();
    #endif // DEBUG
}


void function_two(void)
{
    // doing nothing
}


The number of spaces that precede the macros seem to influence the
strange behavior.

At first I thought that the c-*-of-defun functions worked if both
C-macros were aligned, but it is not always the case.

It is quite easy to see if the current state is working or not: the
identifier 'function_two' is not colored correctly when the function
recognition fails.

There seems also to be differences depending on the form of the macro:
#if defined(DEBUG)
#if defined DEBUG
#ifdef DEBUG

The cases showing the fault depend on the previous states, the file
content alone is not enough to guarantee seeing the fault.

Examples of code that causes the fault (only function_one is shown for
readability, see above for function_two):

--- 1 ---
1.a.
// -- OK
void function_one(void)
{
    // Make the number of spaces before # vary.
#if defined DEBUG
    trap();
#endif // DEBUG
}

1.b. Add one space before #if -> Fail
1.c. Add one more space before #if -> OK
1.d. Delete the space added in 1.c. -> still OK (although same content
as in 1.b.).


--- 2 ---
2.a.
// --- OK
void function_one(void)
{
    // Make the number of spaces before # vary.
#if defined DEBUG
    trap();
#endif // DEBUG
}

2.b. Add one space before #if -> OK (unlike case 1)
2.c. Add one more space before #if -> Fail
2.d. Delete the space added in 2.c. -> still Fail (although same
content as in 2.b.).
2.e. Remove the last space before #if -> OK


--- 3 ---
3.a.
// --- OK
void function_one(void)
{
    // Make the number of spaces before # vary.
#if defined DEBUG
    trap();
#endif // DEBUG
}

3.b. Add four spaces before #if -> Fail
3.c. Add one space before #endif -> Fail
3.d. Add one space before #endif -> OK
3.e. Delete one space before #endif -> OK (although same content as 3.c)


As far as I can see, the behavior of #ifdef is the same as that of #if
defined without parens. The behavior of #if defined with parens
differs, though.

IIRC, all preprocessor macros should be considered as blank lines when
it comes to function recognition?



^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#14133: 24.2; c functions recognition breaks on certain preprocessor macros
  2013-04-03 10:48 bug#14133: 24.2; c functions recognition breaks on certain preprocessor macros Gauthier Ostervall
@ 2013-04-09  6:33 ` Gauthier Östervall
  2013-04-09 21:06 ` Alan Mackenzie
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Gauthier Östervall @ 2013-04-09  6:33 UTC (permalink / raw)
  To: 14133

On Wed, Apr 3, 2013 at 12:48 PM, Gauthier Ostervall
<gauthier@ostervall.se> wrote:
> Function coloring, c-beginning-of-defun and c-end-of-defun behaves
> strangely if the functions contain ifdef'd code.
>

> bug#14133: 24.2;

Faulty behavior also verified on 24.3.1
Working as expected on 23.4.1

All version installed from the zip files at
http://ftp.gnu.org/gnu/emacs/windows/ , on Windows 7.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#14133: 24.2; c functions recognition breaks on certain preprocessor macros
  2013-04-03 10:48 bug#14133: 24.2; c functions recognition breaks on certain preprocessor macros Gauthier Ostervall
  2013-04-09  6:33 ` Gauthier Östervall
@ 2013-04-09 21:06 ` Alan Mackenzie
       [not found] ` <20130409210621.GA3571@acm.acm>
  2013-04-15 16:19 ` Alan Mackenzie
  3 siblings, 0 replies; 5+ messages in thread
From: Alan Mackenzie @ 2013-04-09 21:06 UTC (permalink / raw)
  To: 14133

Hi, Gauthier.

> Function coloring, c-beginning-of-defun and c-end-of-defun behaves
> strangely if the functions contain ifdef'd code.

More precisely, doing C-M-a from function_two goes to the beginning of
function_one.  I couldn't see any fault with the font locking, though.

> For example:
> void function_one(void)
> {
>     // Make the number of spaces before # vary.
>     #if defined(DEBUG)
>     trap();
>     #endif // DEBUG
> }
>
>
> void function_two(void)
> {
>     // doing nothing
> }


> The number of spaces that precede the macros seem to influence the
> strange behavior.

Actually, it is the prescence of spaces before the # which are triggering
the bug.

> It is quite easy to see if the current state is working or not: the
> identifier 'function_two' is not colored correctly when the function
> recognition fails.

I didn't see this.  Would you please try the patch below and let me know
how much it fixes and how much is still not working.  After applying the
patch, you MUST byte-compile cc-langs.el, cc-engine.el and cc-mode.el,
for it to work.  (Let me know if you have any trouble doing this.)

Thanks for taking the trouble to report the bug.  Here's the patch:



=== modified file 'lisp/progmodes/cc-langs.el'
*** lisp/progmodes/cc-langs.el	2013-03-05 17:13:01 +0000
--- lisp/progmodes/cc-langs.el	2013-04-09 20:36:25 +0000
***************
*** 812,819 ****
  (c-lang-defconst c-anchored-cpp-prefix
    "Regexp matching the prefix of a cpp directive anchored to BOL,
  in the languages that have a macro preprocessor."
!   t (if (c-lang-const c-opt-cpp-prefix)
! 	(concat "^" (c-lang-const c-opt-cpp-prefix))))
  (c-lang-defvar c-anchored-cpp-prefix (c-lang-const c-anchored-cpp-prefix))
  
  (c-lang-defconst c-opt-cpp-start
--- 812,819 ----
  (c-lang-defconst c-anchored-cpp-prefix
    "Regexp matching the prefix of a cpp directive anchored to BOL,
  in the languages that have a macro preprocessor."
!   t "^\\s *\\(#\\)\\s *"
!   (java awk) nil)
  (c-lang-defvar c-anchored-cpp-prefix (c-lang-const c-anchored-cpp-prefix))
  
  (c-lang-defconst c-opt-cpp-start

=== modified file 'lisp/progmodes/cc-mode.el'
*** lisp/progmodes/cc-mode.el	2013-01-02 16:13:04 +0000
--- lisp/progmodes/cc-mode.el	2013-04-09 20:39:18 +0000
***************
*** 936,942 ****
  
      ;; Add needed properties to each CPP construct in the region.
      (goto-char c-new-BEG)
!     (let ((pps-position c-new-BEG)  pps-state mbeg)
        (while (and (< (point) c-new-END)
  		  (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
  	;; If we've found a "#" inside a string/comment, ignore it.
--- 936,943 ----
  
      ;; Add needed properties to each CPP construct in the region.
      (goto-char c-new-BEG)
!     (skip-chars-backward " \t")
!     (let ((pps-position (point))  pps-state mbeg)
        (while (and (< (point) c-new-END)
  		  (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
  	;; If we've found a "#" inside a string/comment, ignore it.
***************
*** 945,958 ****
  	      pps-position (point))
  	(unless (or (nth 3 pps-state)	; in a string?
  		    (nth 4 pps-state))	; in a comment?
! 	  (goto-char (match-beginning 0))
  	  (setq mbeg (point))
  	  (if (> (c-syntactic-end-of-macro) mbeg)
  	      (progn
  		(c-neutralize-CPP-line mbeg (point))
! 		(c-set-cpp-delimiters mbeg (point))
! 		;(setq pps-position (point))
! 		)
  	    (forward-line))	      ; no infinite loop with, e.g., "#//"
  	  )))))
  
--- 946,957 ----
  	      pps-position (point))
  	(unless (or (nth 3 pps-state)	; in a string?
  		    (nth 4 pps-state))	; in a comment?
! 	  (goto-char (match-beginning 1))
  	  (setq mbeg (point))
  	  (if (> (c-syntactic-end-of-macro) mbeg)
  	      (progn
  		(c-neutralize-CPP-line mbeg (point))
! 		(c-set-cpp-delimiters mbeg (point)))
  	    (forward-line))	      ; no infinite loop with, e.g., "#//"
  	  )))))
  


-- 
Alan Mackenzie (Nuremberg, Germany).





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#14133: 24.2; c functions recognition breaks on certain preprocessor macros
       [not found] ` <20130409210621.GA3571@acm.acm>
@ 2013-04-10 11:49   ` Gauthier Östervall
  0 siblings, 0 replies; 5+ messages in thread
From: Gauthier Östervall @ 2013-04-10 11:49 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: 14133

On Tue, Apr 9, 2013 at 11:06 PM, Alan Mackenzie <acm@muc.de> wrote:
> Would you please try the patch below and let me know
> how much it fixes and how much is still not working.  After applying the
> patch, you MUST byte-compile cc-langs.el, cc-engine.el and cc-mode.el,
> for it to work.  (Let me know if you have any trouble doing this.)

Your patch fixed this issue, thank you! (built in Cygwin)
Now I will need to find out how to build emacs natively for win64 (or
revert to version 23 and wait for your correction to be integrated).





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#14133: 24.2; c functions recognition breaks on certain preprocessor macros
  2013-04-03 10:48 bug#14133: 24.2; c functions recognition breaks on certain preprocessor macros Gauthier Ostervall
                   ` (2 preceding siblings ...)
       [not found] ` <20130409210621.GA3571@acm.acm>
@ 2013-04-15 16:19 ` Alan Mackenzie
  3 siblings, 0 replies; 5+ messages in thread
From: Alan Mackenzie @ 2013-04-15 16:19 UTC (permalink / raw)
  To: 14133-done

Bug fixed in the trunk.

-- 
Alan Mackenzie (Nuremberg, Germany).





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-04-15 16:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-03 10:48 bug#14133: 24.2; c functions recognition breaks on certain preprocessor macros Gauthier Ostervall
2013-04-09  6:33 ` Gauthier Östervall
2013-04-09 21:06 ` Alan Mackenzie
     [not found] ` <20130409210621.GA3571@acm.acm>
2013-04-10 11:49   ` Gauthier Östervall
2013-04-15 16:19 ` Alan Mackenzie

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.