GNU bug report logs - #4289
23.1; Incorrect indentation in C, following certain 'case' labels

Previous Next

Packages: emacs, cc-mode;

Reported by: Benjamin Moody <benjamin.moody <at> gmail.com>

Date: Sat, 29 Aug 2009 21:05:05 UTC

Severity: normal

Done: Juanma Barranquero <lekktu <at> gmail.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 4289 in the body.
You can then email your comments to 4289 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>:
bug#4289; Package emacs. (Sat, 29 Aug 2009 21:05:05 GMT) Full text and rfc822 format available.

Acknowledgement sent to Benjamin Moody <benjamin.moody <at> gmail.com>:
New bug report received and forwarded. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>. (Sat, 29 Aug 2009 21:05:06 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Benjamin Moody <benjamin.moody <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 23.1; Incorrect indentation in C, following certain 'case' labels
Date: Sat, 29 Aug 2009 16:59:54 -0400


In Emacs 23.1, I've found an issue with the indentation rules for C
mode.  In some circumstances, following a series of multiple labels,
the second and subsequent statements are not indented properly.  For
example, in the following code:

void foo(int x)
{
  switch (x) {
  case 1:
  case 2:
    printf("aaa");
    break;

  case '3':
  case '4':
    printf("bbb");
  break;
  }
}

the second 'break' statement should be indented by 4 spaces, to align
with the 'printf'.

This problem seems to appear when there are multiple labels (either
normal named labels, 'case' labels, or 'default' labels) in a row,
with no intervening statments, and either the first or the last such
label is a 'case' label with a character constant as its value.

This problem does NOT occur with Emacs 22.3.  It does occur in C++ and
ObjC modes as well.  It affects indentation using either TAB or M-x
indent-region.

A workaround is to place the character constant(s) in parentheses.

This is obviously not a serious problem, but it is a somewhat annoying
one which would be nice to see fixed.

Benjamin Moody



In GNU Emacs 23.1.1 (x86_64-pc-linux-gnu, X toolkit, Xaw3d scroll bars)
 of 2009-08-03 on nautilus, modified by Debian
Windowing system distributor `The X.Org Foundation', version 11.0.10402000
configured using `configure  '--build=x86_64-linux-gnu' '--host=x86_64-linux-gnu' '--prefix=/usr' '--sharedstatedir=/var/lib' '--libexecdir=/usr/lib' '--localstatedir=/var/lib' '--infodir=/usr/share/info' '--mandir=/usr/share/man' '--with-pop=yes' '--enable-locallisppath=/etc/emacs23:/etc/emacs:/usr/local/share/emacs/23.1/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/23.1/site-lisp:/usr/share/emacs/site-lisp:/usr/share/emacs/23.1/leim' '--with-x=yes' '--with-x-toolkit=athena' '--with-toolkit-scroll-bars' 'build_alias=x86_64-linux-gnu' 'host_alias=x86_64-linux-gnu' 'CFLAGS=-DDEBIAN -g -O2' 'LDFLAGS=-g' 'CPPFLAGS=''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: nil
  locale-coding-system: utf-8-unix
  default-enable-multibyte-characters: t

Major mode: C/l

Minor modes in effect:
  iswitchb-mode: t
  tooltip-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  global-auto-composition-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  column-number-mode: t
  line-number-mode: t
  abbrev-mode: t

Recent input:
<help-echo> C-x C-f / t m p / t e s t . c <return> 
v o i d SPC f o o ( i n t SPC x ) <return> { <return> 
<tab> s w i t c h SPC ( x ) SPC { <return> <tab> c 
a s d e <backspace> <backspace> e 1 SPC <backspace> 
<backspace> SPC 1 : <return> <tab> c a s e SPC 2 : 
<return> <tab> p r i n t f ( " a a a " ) ; <return> 
<tab> b r e a k ; <return> <return> <tab> c a s e SPC 
' 3 ' : <return> <tab> c a s e SPC ' 4 ' : <return> 
<tab> p r i n t f ( " b b b " ) ; <return> <tab> b 
r e a k j ; <backspace> <backspace> ; <return> } <S-return> 
} <return> M-x r e p o r t - e m a <tab> <return>

Recent messages:
Setting up ede...
Loading `eieio': old-style backquotes detected!
Setting up ede...done
Setting up eieio...done
Setting up semantic...done
Setting up speedbar...done
Setting up cedet-contrib...done
Loading /etc/emacs/site-start.d/55ecb.el (source)...done
For information about GNU Emacs and the GNU system, type C-h C-a.
(New file)




bug reassigned from package 'emacs' to 'emacs,cc-mode'. Request was from Glenn Morris <rgm <at> gnu.org> to control <at> emacsbugs.donarmstrong.com. (Mon, 31 Aug 2009 01:45:04 GMT) Full text and rfc822 format available.

Information forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>, owner <at> emacsbugs.donarmstrong.com:
bug#4289; Package emacs,cc-mode. (Mon, 31 Aug 2009 20:10:05 GMT) Full text and rfc822 format available.

Acknowledgement sent to Alan Mackenzie <acm <at> muc.de>:
Extra info received and forwarded to list. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>, owner <at> emacsbugs.donarmstrong.com. (Mon, 31 Aug 2009 20:10:05 GMT) Full text and rfc822 format available.

Message #12 received at submit <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Benjamin Moody <benjamin.moody <at> gmail.com>, 4289 <at> debbugs.gnu.org
Cc: bug-gnu-emacs <at> gnu.org
Subject: Re: bug#4289: 23.1;
	Incorrect indentation in C, following certain 'case' labels
Date: Mon, 31 Aug 2009 20:05:38 +0000
Hi, Benjamin!

On Sat, Aug 29, 2009 at 04:59:54PM -0400, Benjamin Moody wrote:



> In Emacs 23.1, I've found an issue with the indentation rules for C
> mode.  In some circumstances, following a series of multiple labels,
> the second and subsequent statements are not indented properly.  For
> example, in the following code:

> void foo(int x)
> {
>   switch (x) {
>   case 1:
>   case 2:
>     printf("aaa");
>     break;

>   case '3':
>   case '4':
>     printf("bbb");
>   break;
>   }
> }

> the second 'break' statement should be indented by 4 spaces, to align
> with the 'printf'.

Indeed it should.

> This problem seems to appear when there are multiple labels (either
> normal named labels, 'case' labels, or 'default' labels) in a row,
> with no intervening statments, and either the first or the last such
> label is a 'case' label with a character constant as its value.

> This problem does NOT occur with Emacs 22.3.  It does occur in C++ and
> ObjC modes as well.  It affects indentation using either TAB or M-x
> indent-region.

> A workaround is to place the character constant(s) in parentheses.

Yuck!

> This is obviously not a serious problem, ...

I might disagree about that.

> ... but it is a somewhat annoying one which would be nice to see fixed.

OK.  Would you please test the patch below, which I think fixes it.  Once
you've applied the patch, please rebuild the CC Mode files as follows:

M-x byte-compile-file cc-langs.el
M-x byte-compile-file cc-engine.el
M-x byte-compile-file cc-mode.el

(This is needed because cc-{engine,mode}.el use macros contained in
cc-langs.el.)  Then restart Emacs if necessary, then test your file.

Thank you indeed for such a wonderfully clear, concise and helpful bug
report!


Index: cc-engine.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/cc-engine.el,v
retrieving revision 1.76
diff -c -r1.76 cc-engine.el
*** cc-engine.el	29 Aug 2009 02:07:45 -0000	1.76
--- cc-engine.el	31 Aug 2009 19:55:19 -0000
***************
*** 1067,1074 ****
  		 (not (eq ret 'beginning))
  		 (looking-at c-case-kwds-regexp))
  	(if (< after-case:-pos start)
! 	    (setq pos after-case:-pos)
! 	  (setq ret 'label)))
  
        ;; Skip over the unary operators that can start the statement.
        (while (progn
--- 1067,1075 ----
  		 (not (eq ret 'beginning))
  		 (looking-at c-case-kwds-regexp))
  	(if (< after-case:-pos start)
! 	    (setq pos after-case:-pos))
! 	(if (eq ret 'same)
! 	    (setq ret 'label)))
  
        ;; Skip over the unary operators that can start the statement.
        (while (progn
Index: cc-langs.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/cc-langs.el,v
retrieving revision 1.67
diff -c -r1.67 cc-langs.el
*** cc-langs.el	19 May 2009 22:35:07 -0000	1.67
--- cc-langs.el	31 Aug 2009 19:55:21 -0000
***************
*** 2885,2891 ****
  i.e. before \":\".  Only used if `c-recognize-colon-labels' is set."
    t (concat
       ;; Don't allow string literals.
!      "[\"']\\|"
       ;; All keywords except `c-label-kwds' and `c-protection-kwds'.
       (c-make-keywords-re t
         (set-difference (c-lang-const c-keywords)
--- 2885,2891 ----
  i.e. before \":\".  Only used if `c-recognize-colon-labels' is set."
    t (concat
       ;; Don't allow string literals.
!      "\"\\|"
       ;; All keywords except `c-label-kwds' and `c-protection-kwds'.
       (c-make-keywords-re t
         (set-difference (c-lang-const c-keywords)

> Benjamin Moody

-- 
Alan Mackenzie (Nuremberg, Germany)







Information forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>, owner <at> emacsbugs.donarmstrong.com:
bug#4289; Package emacs,cc-mode. (Mon, 31 Aug 2009 20:10:07 GMT) Full text and rfc822 format available.

Acknowledgement sent to Alan Mackenzie <acm <at> muc.de>:
Extra info received and forwarded to list. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>, owner <at> emacsbugs.donarmstrong.com. (Mon, 31 Aug 2009 20:10:07 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to Benjamin Moody <benjamin.moody <at> gmail.com> Request was from Juanma Barranquero <lekktu <at> gmail.com> to control <at> emacsbugs.donarmstrong.com. (Thu, 22 Oct 2009 09:40:28 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> emacsbugs.donarmstrong.com. (Thu, 19 Nov 2009 15:24:18 GMT) Full text and rfc822 format available.

This bug report was last modified 15 years and 218 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.