all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* c preprocessor and indent problem in c-mode?
@ 2009-01-26  0:46 YOUNG
  2009-01-26  2:03 ` YOUNG
  2009-01-26  9:00 ` Alan Mackenzie
  0 siblings, 2 replies; 3+ messages in thread
From: YOUNG @ 2009-01-26  0:46 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

If there is a following code, emacs does not present indent correctly.

#incude <stdio.h>

int main(void) {
  int a = 10;
#ifdef AAA
  if (a == 10) {
    printf("a:%d\n", a);
#else
    if (b == 10) {
      printf("b=10\n");
#endif
    }

#endif
  }

If the curly brace is not closed before the preprocessor, emacs seems
to confuse to make indent.

Is there anyone who know this problem and how to resolve?


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

* Re: c preprocessor and indent problem in c-mode?
  2009-01-26  0:46 c preprocessor and indent problem in c-mode? YOUNG
@ 2009-01-26  2:03 ` YOUNG
  2009-01-26  9:00 ` Alan Mackenzie
  1 sibling, 0 replies; 3+ messages in thread
From: YOUNG @ 2009-01-26  2:03 UTC (permalink / raw)
  To: help-gnu-emacs

On Jan 25, 4:46 pm, YOUNG <breadn...@gmail.com> wrote:
> Hi,
>
> If there is a following code, emacs does not present indent correctly.
>
> #incude <stdio.h>
>
> int main(void) {
>   int a = 10;
> #ifdef AAA
>   if (a == 10) {
>     printf("a:%d\n", a);
> #else
>     if (b == 10) {
>       printf("b=10\n");
> #endif
>     }
>
> #endif
>   }
>
> If the curly brace is not closed before the preprocessor, emacs seems
> to confuse to make indent.
>
> Is there anyone who know this problem and how to resolve?

The code I wrote before was not completed, but this is the final one.

#include <stdio.h>

int main(void) {
  int a = 10;
  int b=10;
#ifdef AAA
  if (a == 10) {
    printf("a:%d\n", a);
#else
    if (b == 10) {
      printf("b=10\n");
#endif
    }
  }

And, as you could see, emacs seems to confuse the indent the code.


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

* Re: c preprocessor and indent problem in c-mode?
  2009-01-26  0:46 c preprocessor and indent problem in c-mode? YOUNG
  2009-01-26  2:03 ` YOUNG
@ 2009-01-26  9:00 ` Alan Mackenzie
  1 sibling, 0 replies; 3+ messages in thread
From: Alan Mackenzie @ 2009-01-26  9:00 UTC (permalink / raw)
  To: YOUNG; +Cc: help-gnu-emacs

Hi, there,

On Sun, Jan 25, 2009 at 04:46:38PM -0800, YOUNG wrote:
> Hi,

> If there is a following code, emacs does not present indent correctly.

> #incude <stdio.h>

> int main(void) {
>   int a = 10;
> #ifdef AAA
>   if (a == 10) {       <====================== A
>     printf("a:%d\n", a);
> #else
>     if (b == 10) {       <====================== B
>       printf("b=10\n");
> #endif
>     }

> #endif
>   }

> If the curly brace is not closed before the preprocessor, emacs seems
> to confuse to make indent.

Yes.

> Is there anyone who know this problem and how to resolve?

Yes.  CC Mode doesn't take account of the #ifdef and #else when parsing
braces.  So it looks like the brace on line B is nested within the brace
on line A.  This is a longstanding bug, which needs fundamental work in
CC Mode (and possibly the Emacs core) to fix.  It may get fixed in the
medium future.  Sorry!

A reasonable workaround, if the project you're working on will let you,
is to keep braces in #ifdefs (and #defines) balanced.  You could thus
write this instead:

#ifdef AAA
  if (a == 10) {
    printf("a:%d\n", a);
  }
#else
  if (b == 10) {
    printf("b=10\n");
  }    
#endif

-- 
Alan Mackenzie (Nuremberg, Germany).




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

end of thread, other threads:[~2009-01-26  9:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-26  0:46 c preprocessor and indent problem in c-mode? YOUNG
2009-01-26  2:03 ` YOUNG
2009-01-26  9:00 ` 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.