unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#17756: indentation after declaring+initializing two arrays
@ 2014-06-11 14:26 ` Pietro Belotti
  2014-06-12 19:25   ` Alan Mackenzie
  0 siblings, 1 reply; 4+ messages in thread
From: Pietro Belotti @ 2014-06-11 14:26 UTC (permalink / raw)
  To: 17756

Hello. I have a problem with Emacs (version: 23.4.1
(x86_64-pc-linux-gnu, GTK+ Version 2.24.10) of 2012-09-08 on trouble,
modified by Debian [2 times]). A declaration is incorrectly indented
when following one where two or more arrays are declared and
initialized. The function foo() below has "char c = 'a';"
over-indented, while the function bar() following it has the same
declaration correctly indented.

int foo () {

  int a [2] = {1,2},
    b [2] = {3,4};

    char c = 'a';
}

int bar () {

  int
    b [2] = {3,4};

  char c = 'a';
}

This also happens if a newline is placed between "int" and "a [2]",
but it does not happen if a and b are declared on the same line. Both
M-x indent-region and moving the cursor and hitting TAB result in
this. I could not find any related issue in the mailing list archive,
is this expected behaviour?

Thanks,
Pietro





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

* bug#17756: indentation after declaring+initializing two arrays
  2014-06-11 14:26 ` bug#17756: indentation after declaring+initializing two arrays Pietro Belotti
@ 2014-06-12 19:25   ` Alan Mackenzie
  0 siblings, 0 replies; 4+ messages in thread
From: Alan Mackenzie @ 2014-06-12 19:25 UTC (permalink / raw)
  To: gnu-emacs-bug

Pietro Belotti <petr.7b6@gmail.com> wrote:
> Hello. I have a problem with Emacs (version: 23.4.1
> (x86_64-pc-linux-gnu, GTK+ Version 2.24.10) of 2012-09-08 on trouble,
> modified by Debian [2 times]). A declaration is incorrectly indented
> when following one where two or more arrays are declared and
> initialized. The function foo() below has "char c = 'a';"
> over-indented, while the function bar() following it has the same
> declaration correctly indented.

> int foo () {
>
>  int a [2] = {1,2},
>    b [2] = {3,4};
>
>    char c = 'a';
> }

> This also happens if a newline is placed between "int" and "a [2]",
> but it does not happen if a and b are declared on the same line. Both
> M-x indent-region and moving the cursor and hitting TAB result in
> this. I could not find any related issue in the mailing list archive,
> is this expected behaviour?

No, it's a bug.  What is happening is that C Mode has got confused by the
brace expression "{1,2}" into thinking that a statement ends after
"{1,2},", because close braces are usually ends of statements.  Hence it
thinks that "b [2] ...." is the beginning of a statement.  The next
statement "char c ..." is thus indented under the "b [2] ...".

I'll get a fix to this out soon.  Thanks for taking the trouble to report
this bug.

By the way, for future reference, it is helpful if you say what major mode
you're using (the best place being in the Subject:) so that we don't have
to guess.

> Thanks,
> Pietro

-- 
Alan Mackenzie (Nuremberg, Germany).






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

* bug#17756: indentation after declaring+initializing two arrays
       [not found] ` <lncurr$108d$1@colin.muc.de>
@ 2014-06-16 21:31   ` Alan Mackenzie
  2014-08-02 20:49     ` Alan Mackenzie
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Mackenzie @ 2014-06-16 21:31 UTC (permalink / raw)
  To: Pietro Belotti; +Cc: 17756

Hi, Pietro.

On Thu, Jun 12, 2014 at 07:25:47PM +0000, Alan Mackenzie wrote:
> Pietro Belotti <petr.7b6@gmail.com> wrote:
> > Hello. I have a problem with Emacs (version: 23.4.1
> > (x86_64-pc-linux-gnu, GTK+ Version 2.24.10) of 2012-09-08 on trouble,
> > modified by Debian [2 times]). A declaration is incorrectly indented
> > when following one where two or more arrays are declared and
> > initialized. The function foo() below has "char c = 'a';"
> > over-indented, while the function bar() following it has the same
> > declaration correctly indented.

> > int foo () {

> >  int a [2] = {1,2},
> >    b [2] = {3,4};

> >    char c = 'a';
> > }

> > This also happens if a newline is placed between "int" and "a [2]",
> > but it does not happen if a and b are declared on the same line. Both
> > M-x indent-region and moving the cursor and hitting TAB result in
> > this. I could not find any related issue in the mailing list archive,
> > is this expected behaviour?

> No, it's a bug.  What is happening is that C Mode has got confused by the
> brace expression "{1,2}" into thinking that a statement ends after
> "{1,2},", because close braces are usually ends of statements.  Hence it
> thinks that "b [2] ...." is the beginning of a statement.  The next
> statement "char c ..." is thus indented under the "b [2] ...".

Incidentally, in that snippet, "b" wasn't getting fontified either.

I've prepared a fix for this, but unfortunately, I couldn't test it on
Emacs 23.4; I built that version, but it hung at 100% CPU usage at
startup.

Nevertheless, perhaps you could try out the following (untested on 23.4)
patch (which _isn't_ valid for the trunk), and let me know whether it
fixes the problem.  Thanks!



*** cc-engine.el~	2012-01-11 12:35:01.000000000 +0000
--- cc-engine.el	2014-06-15 16:21:23.000000000 +0000
***************
*** 943,949 ****
  
  				  ((and
  				    (eq (char-after) ?{)
! 				    (not (c-looking-at-inexpr-block lim nil t)))
  				   ;; Passed a block sexp.  That's a boundary
  				   ;; alright.
  				   (point))
--- 943,952 ----
  
  				  ((and
  				    (eq (char-after) ?{)
! 				    (not (c-looking-at-inexpr-block lim nil t))
! 				    (save-excursion
! 				      (c-backward-token-2 1 t nil)
! 				      (not (looking-at "=\\([^=]\\|$\\)"))))
  				   ;; Passed a block sexp.  That's a boundary
  				   ;; alright.
  				   (point))
***************
*** 8759,8765 ****
  	    (if (eq (point) (c-point 'boi))
  		(c-add-syntax 'brace-list-close (point))
  	      (setq lim (c-most-enclosing-brace c-state-cache (point)))
! 	      (c-beginning-of-statement-1 lim)
  	      (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
  
  	   (t
--- 8762,8768 ----
  	    (if (eq (point) (c-point 'boi))
  		(c-add-syntax 'brace-list-close (point))
  	      (setq lim (c-most-enclosing-brace c-state-cache (point)))
! 	      (c-beginning-of-statement-1 lim nil nil t)
  	      (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
  
  	   (t
*** cc-fonts.el~	2012-01-11 12:35:01.000000000 +0000
--- cc-fonts.el	2014-06-15 16:28:33.000000000 +0000
***************
*** 841,847 ****
         paren-depth
         id-face got-init
         c-last-identifier-range
!        (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
  
      (while (and
  	    pos
--- 841,848 ----
         paren-depth
         id-face got-init
         c-last-identifier-range
!        (separator-prop (if types 'c-decl-type-start 'c-decl-id-start))
!        brackets-after-id)
  
      (while (and
  	    pos
***************
*** 899,911 ****
  	    ;; Search syntactically to the end of the declarator (";",
  	    ;; ",", a closen paren, eob etc) or to the beginning of an
  	    ;; initializer or function prototype ("=" or "\\s\(").
! 	    ;; Note that the open paren will match array specs in
! 	    ;; square brackets, and we treat them as initializers too.
! 	    (c-syntactic-re-search-forward
! 	     "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
  
        (setq next-pos (match-beginning 0)
  	    id-face (if (and (eq (char-after next-pos) ?\()
  			     (let (c-last-identifier-range)
  			       (save-excursion
  				 (goto-char next-pos)
--- 900,923 ----
  	    ;; Search syntactically to the end of the declarator (";",
  	    ;; ",", a closen paren, eob etc) or to the beginning of an
  	    ;; initializer or function prototype ("=" or "\\s\(").
! 	    ;; Note that square brackets are now not also treated as
! 	    ;; initializers, since this broke when there were also
! 	    ;; initializing brace lists.
! 	    (let (found)
! 	      (while
! 	    	  (and (setq found
! 	    		     (c-syntactic-re-search-forward
! 	    		      "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
! 	    	       (eq (char-before) ?\[))
! 	    	(backward-char)
! 	    	(c-safe (c-forward-sexp 1))
! 	    	(setq found nil)
! 	    	(setq brackets-after-id t))
! 	      found))
  
        (setq next-pos (match-beginning 0)
  	    id-face (if (and (eq (char-after next-pos) ?\()
+ 			     (not brackets-after-id)
  			     (let (c-last-identifier-range)
  			       (save-excursion
  				 (goto-char next-pos)



> > Thanks,
> > Pietro

-- 
Alan Mackenzie (Nuremberg, Germany).





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

* bug#17756: indentation after declaring+initializing two arrays
  2014-06-16 21:31   ` Alan Mackenzie
@ 2014-08-02 20:49     ` Alan Mackenzie
  0 siblings, 0 replies; 4+ messages in thread
From: Alan Mackenzie @ 2014-08-02 20:49 UTC (permalink / raw)
  To: 17756-done

Bug fixed.

-- 
Alan Mackenzie (Nuremberg, Germany).





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

end of thread, other threads:[~2014-08-02 20:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <mailman.3424.1402501025.1147.bug-gnu-emacs@gnu.org>
2014-06-11 14:26 ` bug#17756: indentation after declaring+initializing two arrays Pietro Belotti
2014-06-12 19:25   ` Alan Mackenzie
     [not found] ` <lncurr$108d$1@colin.muc.de>
2014-06-16 21:31   ` Alan Mackenzie
2014-08-02 20:49     ` Alan Mackenzie

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).