From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Simon Newsgroups: gmane.emacs.bugs Subject: bug#19867: c++-mode indentation issues with C++1x initializer lists Date: Sat, 14 Feb 2015 12:50:31 -0500 Message-ID: Reply-To: turner25@gmail.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=047d7b10cef340cb22050f0ffd4c X-Trace: ger.gmane.org 1423937848 15039 80.91.229.3 (14 Feb 2015 18:17:28 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 14 Feb 2015 18:17:28 +0000 (UTC) To: 19867@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Feb 14 19:17:16 2015 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YMhHG-0004ru-UO for geb-bug-gnu-emacs@m.gmane.org; Sat, 14 Feb 2015 19:17:15 +0100 Original-Received: from localhost ([::1]:60824 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YMhHG-0003kH-2I for geb-bug-gnu-emacs@m.gmane.org; Sat, 14 Feb 2015 13:17:14 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56292) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YMhH7-0003jA-NM for bug-gnu-emacs@gnu.org; Sat, 14 Feb 2015 13:17:11 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YMhH4-0000Ix-Gl for bug-gnu-emacs@gnu.org; Sat, 14 Feb 2015 13:17:05 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:52888) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YMhH4-0000It-D9 for bug-gnu-emacs@gnu.org; Sat, 14 Feb 2015 13:17:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1YMhH4-0005jF-5k for bug-gnu-emacs@gnu.org; Sat, 14 Feb 2015 13:17:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Simon Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 14 Feb 2015 18:17:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 19867 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: X-Debbugs-Original-To: submit@debbugs.gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.142393779621983 (code B ref -1); Sat, 14 Feb 2015 18:17:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 14 Feb 2015 18:16:36 +0000 Original-Received: from localhost ([127.0.0.1]:44128 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YMhGd-0005iT-FR for submit@debbugs.gnu.org; Sat, 14 Feb 2015 13:16:36 -0500 Original-Received: from mail-pd0-f181.google.com ([209.85.192.181]:38868) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YMgrV-00057B-7w for submit@debbugs.gnu.org; Sat, 14 Feb 2015 12:50:38 -0500 Original-Received: by pdbfp1 with SMTP id fp1so21716002pdb.5 for ; Sat, 14 Feb 2015 09:50:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:date:message-id:subject:from:to:content-type; bh=U6KJg7bMGE5psLP4fmFFgrilFisMYrb3V5KrGL1vE6E=; b=dixcabT3ungkWGJmnmfwd8WvFc1mqISUfA/pEnIn06B93MWAjlXquNGaUh1ExACAVx UdvlXQO8lg9Bx33fVtQc74aG/lk/pU2hEdqQavT09XrqhvjNFBJJnmZhKBn2T/CqF8fm Q//J9g7bOA3VF73MRnBfXP1DEX83WPvhrJxS1kRpfuvzxDb0kKILNYmF6oVA/8u4B/7W TsVMYsL29BaOGYsE84umJPs8nQEvuGAanS1yE0wCC2+uFzctlVSx2TzMMsNH8WbcYpkY 74VsotNNKm3/GuDJX3ZUwlYgNU03JF0fCcnHBpcLMyHQ5TP6BGceGl0Xn7PqH74Un0s6 JLbg== X-Received: by 10.68.200.97 with SMTP id jr1mr25029773pbc.123.1423936231359; Sat, 14 Feb 2015 09:50:31 -0800 (PST) Original-Received: by 10.70.112.195 with HTTP; Sat, 14 Feb 2015 09:50:31 -0800 (PST) X-Mailman-Approved-At: Sat, 14 Feb 2015 13:16:33 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:99405 Archived-At: --047d7b10cef340cb22050f0ffd4c Content-Type: text/plain; charset=UTF-8 Package: emacs Version: 24.4.1 Severity: important Related: c++-mode Initializer lists use curly braces, but their contents do not indent properly with emacs' c++-mode. In short, one may use an initializer list to declare and initialize a vector of integers as such: std::vector Foo( { 1, 2, 3, 4, 5 } ); Problems arise when the elements of the list span on multiple line and it gets even worse when the elements are lambda-expressions and nested initializer lists. The following code illustrate most cases and related situations. The code below compiles without error or warning with gcc 4.8.3. In case email systems mess with the spaces, the code below is available at this URL as well: http://next.n32.ca/emacs_initlist_indentation_bug.txt #include #include namespace emacs_initlist_indentation_bug { struct ABC { int a; // OK, text-book indentation int b; // int c; // }; struct DEF { int d, // e, // indented from "int" + 2 f; // }; struct GHI { int // g, // indented from "int" +0 h, // indented from "int" + 2 i; // }; int f1 ( int a, // Indentation OK in function declaration context int b, // int c // ) // Notice how the ")" is indented { if(a>0){ return a+ // while out-of-topic, this probably pinpoints b+ // what's going on internally c; // } else if(a<0) { return (a+ // these are well aligned b+ // c); // } else { return (a+ // these are well aligned b+ // c); // } } void f2 (const ABC& abc) { f1(abc.a, // Indentation OK in function call context abc.b, // abc.c); // } void f3 (int a, int b, int c) { f1( f1( a+1, // Indentation OK, text-book example, perfect! b+1, // c+1 ), // f1( a+2, // b+2, // c+2 ), // f1( a+3, // b+3, // c+3 ) // ); // } void f4 (int a, int b, int c) { f2({a+1, // note "{" on same line as "(" b+1, // indented after "{" + 2 c+1} // ); // Bad! ")" indented underneath "(" instead of argument (ie. "{") f2( {a+2, // note "{" on different line as "(" b+2, // indented after "{" + 3 !!! c+2} // ); // OK, ")" indented underneath "{" // Below are some typical indentation I'm getting these days. // The only difference is in the newline on first line (and numerics) std::vector abcList1({{a+1, // b+1, // c+1}, // {a+2, // b+2, // c+2}, // {a+3, // b+3, // c+3} // } // ); // std::vector abcList2( // Source of alignment for closing ")" below {{a+4, // b+4, // c+4}, // {a+5, // b+5, // c+5}, // {a+6, // b+6, // c+6} // } // ); /* Somehow, this one aligns with first line's comment position!! */ } void f5 () { int foo = 0; std::vector< std::function > lambda_initlist_bug({ // [foo](int x) // { // BAD, too indented by 2 positions return x+x; // }, // [foo](int y) // { // BAD, too indented by 2 positions return y+y; // }, // [foo](int z) // { // BAD, too indented by 2 positions return z+z; // } // } ); lambda_initlist_bug.push_back( // [](int p) // OK, text-book indentation { // return p+p; // } // ); /* Aligned with first comment! */ for(auto f_lambda : lambda_initlist_bug){ f_lambda( 123 ); } } } --047d7b10cef340cb22050f0ffd4c Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Package: emacs
Version: 24.4.1
Severity:= important

Related: c++-mode

<= div>
Initializer lists use curly braces, but their contents d= o not indent properly with emacs' c++-mode.
In short, one may= use an initializer list to declare and initialize a vector of integers as = such:
=C2=A0 std::vector<int> Foo( { 1, 2, 3, 4, 5 } );

Problems arise when the elements of the list span on = multiple line and it gets even worse when the elements are lambda-expressio= ns and nested initializer lists.
The following code illustrate mo= st cases and related situations.=C2=A0 The code below compiles without erro= r or warning with gcc 4.8.3.


In cas= e email systems mess with the spaces, the code below is available at this U= RL as well:
http://next.n32.ca/emacs_initlist_indentation_bug.txt
<= /div>

#include <vector>
#include <functional>

namespace emacs_initlist_indentation_bug {

  struct ABC {
    int a;  // OK, text-book indentation
    int b;  //
    int c;  //
  };

  struct DEF {
    int d, //
      e,   // indented from "int" + 2
      f;   //
  };

  struct GHI {
    int   //
    g,    // indented from "int" +0
      h,  // indented from "int" + 2
      i;  //
  };

  int f1 ( int a,  // Indentation OK in function declaration context
	   int b,  //
	   int c   //
	   )       // Notice how the ")" is indented
  {

    if(a>0){

      return a+  // while out-of-topic, this probably pinpoints
	b+       // what's going on internally=20
	c;       //

    } else if(a<0) {

      return (a+  // these are well aligned
	      b+  //
	      c); //

    } else {

      return=20
	(a+  // these are well aligned
	 b+  //
	 c); //

    }     =20
  }

  void f2 (const ABC& abc)
  {
    f1(abc.a,  // Indentation OK in function call context
       abc.b,  //
       abc.c); //
  }

  void f3 (int a, int b, int c)
  {
    f1( f1( a+1,    // Indentation OK, text-book example, perfect!
	    b+1,    //
	    c+1 ),  //
	f1( a+2,    //
	    b+2,    //
	    c+2 ),  //
	f1( a+3,    //
	    b+3,    //
	    c+3 )   //
	);          //
  }

  void f4 (int a, int b, int c)
  {
    f2({a+1,     // note "{" on same line as "("
	  b+1,   // indented after "{" + 2
	  c+1}   //=20
      );         // Bad! ")" indented underneath "(" in=
stead of argument (ie. "{")
   =20
    f2(
       {a+2,     // note "{" on different line as "("
	   b+2,  // indented after "{" + 3 !!!
	   c+2}  //
       );        // OK, ")" indented underneath "{"
 =20
    // Below are some typical indentation I'm getting these days.
    // The only difference is in the newline on first line (and numerics)
   =20
    std::vector<ABC> abcList1({{a+1, //
	    b+1,                     //
	    c+1},                    //
	  {a+2,                      //
	      b+2,                   //
	      c+2},                  //
	    {a+3,                    //
		b+3,                 //
		c+3}                 //
      }                              //
      );                             //
   =20
    std::vector<ABC> abcList2(                     // Source of align=
ment for closing ")" below
			      {{a+4,               //
				    b+4,           //
				    c+4},          //
				  {a+5,            //
				      b+5,         //
				      c+5},        //
				    {a+6,          //
					b+6,       //
					c+6}       //
			      }                    //
						   ); /* Somehow, this one aligns with first line's comment posit=
ion!! */
   =20
  }

 =20
  void f5 ()
  {
    int foo =3D 0;
    std::vector< std::function<int(int)> >
      lambda_initlist_bug({    //
	  [foo](int x)         //
	    {                  // BAD, too indented by 2 positions
	      return x+x;      //
	    },                 //
	    [foo](int y)       //
	      {                // BAD, too indented by 2 positions
		return y+y;    //
	      },               //
	      [foo](int z)     //
		{              // BAD, too indented by 2 positions
		  return z+z;  //
		}              //
	}
	);
 =20
    lambda_initlist_bug.push_back(              //
				  [](int p)     // OK, text-book indentation
				  {             //
				    return p+p; //
				  }             //
						);  /* Aligned with first comment! */
   =20
    for(auto f_lambda : lambda_initlist_bug){
      f_lambda( 123 );
    }
 =20
  }

}

--047d7b10cef340cb22050f0ffd4c--