bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#17756: indentation after declaring+initializing two arrays


From: Alan Mackenzie
Subject: bug#17756: indentation after declaring+initializing two arrays
Date: Mon, 16 Jun 2014 21:31:44 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

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).





reply via email to

[Prev in Thread] Current Thread [Next in Thread]