GNU bug report logs -
#19867
c++-mode indentation issues with C++1x initializer lists
Previous Next
Reported by: turner25 <at> gmail.com
Date: Sat, 14 Feb 2015 18:17:01 UTC
Severity: minor
Found in version 24.4.1
Done: Alan Mackenzie <acm <at> muc.de>
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 19867 in the body.
You can then email your comments to 19867 AT debbugs.gnu.org in the normal way.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#19867
; Package
emacs
.
(Sat, 14 Feb 2015 18:17:02 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
turner25 <at> gmail.com
:
New bug report received and forwarded. Copy sent to
bug-gnu-emacs <at> gnu.org
.
(Sat, 14 Feb 2015 18:17:02 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
[Message part 1 (text/plain, inline)]
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<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-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 <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
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<ABC> abcList1({{a+1, //
b+1, //
c+1}, //
{a+2, //
b+2, //
c+2}, //
{a+3, //
b+3, //
c+3} //
} //
); //
std::vector<ABC> 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<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; //
} //
}
);
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 );
}
}
}
[Message part 2 (text/html, inline)]
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Mon, 16 Feb 2015 19:10:02 GMT)
Full text and
rfc822 format available.
Message #8 received at 19867 <at> debbugs.gnu.org (full text, mbox):
Simon wrote:
> Severity: important
Unless it leads to invalid code (eg Python-style languages), I tend to
think of indentation bugs as minor problems, since it's just cosmetic.
Why do you think this one is important?
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Mon, 16 Feb 2015 19:30:03 GMT)
Full text and
rfc822 format available.
Message #11 received at 19867 <at> debbugs.gnu.org (full text, mbox):
On Mon, Feb 16, 2015 at 2:09 PM, Glenn Morris <rgm <at> gnu.org> wrote:
> Simon wrote:
>
>> Severity: important
>
> Unless it leads to invalid code (eg Python-style languages), I tend to
> think of indentation bugs as minor problems, since it's just cosmetic.
> Why do you think this one is important?
I am inclined to follow your lead. C++ code compiles without errors
as I mentioned.
The bug affects "c++-mode" and since c++-mode mostly (solely?) affects
cosmetics, then the bug makes c++-mode difficult to use. In fact, I
have been searching for alternatives, in vain.
I would suggest keeping severity as "important" if this report can be
assigned more precisely to the "c++-mode" sub-package or group. If
not, then you may downgrade to "minor" as you said.
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Mon, 16 Feb 2015 21:04:02 GMT)
Full text and
rfc822 format available.
Message #14 received at 19867 <at> debbugs.gnu.org (full text, mbox):
[Message part 1 (text/plain, inline)]
On 02/16/2015 11:09 AM, Glenn Morris wrote:
> Simon wrote:
>
>> Severity: important
>
> Unless it leads to invalid code (eg Python-style languages), I tend to
> think of indentation bugs as minor problems, since it's just cosmetic.
> Why do you think this one is important?
Indentation bugs are infuriating because I use electric mode, which
means that even if I fix the indentation, the fixes are undone
automatically.
[signature.asc (application/pgp-signature, attachment)]
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Mon, 16 Feb 2015 21:39:01 GMT)
Full text and
rfc822 format available.
Message #17 received at 19867 <at> debbugs.gnu.org (full text, mbox):
[Message part 1 (text/plain, inline)]
On 02/16/2015 11:29 AM, Simon wrote:
> On Mon, Feb 16, 2015 at 2:09 PM, Glenn Morris <rgm <at> gnu.org> wrote:
>> Simon wrote:
>>
>>> Severity: important
>>
>> Unless it leads to invalid code (eg Python-style languages), I tend to
>> think of indentation bugs as minor problems, since it's just cosmetic.
>> Why do you think this one is important?
>
> I am inclined to follow your lead. C++ code compiles without errors
> as I mentioned.
>
> The bug affects "c++-mode" and since c++-mode mostly (solely?) affects
> cosmetics, then the bug makes c++-mode difficult to use. In fact, I
> have been searching for alternatives, in vain.
>
> I would suggest keeping severity as "important" if this report can be
> assigned more precisely to the "c++-mode" sub-package or group. If
> not, then you may downgrade to "minor" as you said.
OP, your best bet is to dive into cc-engine.el and fix it yourself.
[signature.asc (application/pgp-signature, attachment)]
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Tue, 17 Feb 2015 19:24:01 GMT)
Full text and
rfc822 format available.
Message #20 received at 19867 <at> debbugs.gnu.org (full text, mbox):
In Emacs, we traditionally reserve severity > normal for failure to
build, security issues etc. So I'll downgrade this to normal, but don't
worry, because I have seen very little evidence that the severity makes
any difference to whether something gets fixed. :), and :( too.
Severity set to 'normal' from 'important'
Request was from
Glenn Morris <rgm <at> gnu.org>
to
control <at> debbugs.gnu.org
.
(Tue, 17 Feb 2015 19:24:02 GMT)
Full text and
rfc822 format available.
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Tue, 17 Feb 2015 19:40:02 GMT)
Full text and
rfc822 format available.
Message #25 received at 19867 <at> debbugs.gnu.org (full text, mbox):
> In Emacs, we traditionally reserve severity > normal for failure to
> build, security issues etc. So I'll downgrade this to normal, but don't
> worry, because I have seen very little evidence that the severity makes
> any difference to whether something gets fixed. :), and :( too.
Sounds good. Thank you Glenn.
I hope someone can look into this within a year or two. Ideally,
before C++17 becomes a reality and swizzles the syntax further with
its "concepts lite" and other features.
I also noticed some more strange indentation cases with nested
constructor initialization lists. I'll be available to test any
improvements made and provide more test cases if needed. Thanks
again.
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Mon, 23 Nov 2020 14:15:01 GMT)
Full text and
rfc822 format available.
Message #28 received at 19867 <at> debbugs.gnu.org (full text, mbox):
Simon <turner25 <at> gmail.com> writes:
> 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<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-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.
I had a look at the fairly long example provided here, and AFAICT, the
indentation is incorrect in the below cases (trimmed down from the
original). Some of the examples of incorrect indentation were already
fixed.
Alan, could you perhaps take a look at this and see if this is something
that is fixable? Thanks in advance.
#include <vector>
#include <functional>
namespace emacs_initlist_indentation_bug {
struct DEF {
int d, //
e, // indented from "int" + 2
f; //
};
struct GHI {
int //
g, // indented from "int" +0
h, // indented from "int" + 2
i; //
};
void f4 (int a, int b, int c)
{
std::vector<ABC> abcList2( // Source of
alignment for closing ")" below
{{a+6,
b+6,
c+6}
} //
); /* Somehow, this
one aligns with first line's comment position!! */
}
lambda_initlist_bug.push_back( //
[](int p) // OK, text-book indentation
{ //
return p+p; //
} //
); /* Aligned with
first comment! */
}
}
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Tue, 11 Mar 2025 16:55:02 GMT)
Full text and
rfc822 format available.
Message #31 received at 19867 <at> debbugs.gnu.org (full text, mbox):
Stefan Kangas <stefankangas <at> gmail.com> writes:
> Simon <turner25 <at> gmail.com> writes:
>
>> 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<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-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.
>
> I had a look at the fairly long example provided here, and AFAICT, the
> indentation is incorrect in the below cases (trimmed down from the
> original). Some of the examples of incorrect indentation were already
> fixed.
>
> Alan, could you perhaps take a look at this and see if this is something
> that is fixable? Thanks in advance.
Alan, any chance you could look into the below cases? Thanks in advance.
> #include <vector>
> #include <functional>
>
> namespace emacs_initlist_indentation_bug {
> struct DEF {
> int d, //
> e, // indented from "int" + 2
> f; //
> };
> struct GHI {
> int //
> g, // indented from "int" +0
> h, // indented from "int" + 2
> i; //
> };
>
> void f4 (int a, int b, int c)
> {
> std::vector<ABC> abcList2( // Source of
> alignment for closing ")" below
> {{a+6,
> b+6,
> c+6}
> } //
> ); /* Somehow, this
> one aligns with first line's comment position!! */
> }
>
> lambda_initlist_bug.push_back( //
> [](int p) // OK, text-book indentation
> { //
> return p+p; //
> } //
> ); /* Aligned with
> first comment! */
> }
> }
Severity set to 'minor' from 'normal'
Request was from
Stefan Kangas <stefankangas <at> gmail.com>
to
control <at> debbugs.gnu.org
.
(Tue, 11 Mar 2025 16:55:03 GMT)
Full text and
rfc822 format available.
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Tue, 11 Mar 2025 20:16:02 GMT)
Full text and
rfc822 format available.
Message #36 received at 19867 <at> debbugs.gnu.org (full text, mbox):
Hello, Stefan.
On Tue, Mar 11, 2025 at 09:54:20 -0700, Stefan Kangas wrote:
> Stefan Kangas <stefankangas <at> gmail.com> writes:
> > Simon <turner25 <at> gmail.com> writes:
> >> 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<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-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.
> > I had a look at the fairly long example provided here, and AFAICT, the
> > indentation is incorrect in the below cases (trimmed down from the
> > original). Some of the examples of incorrect indentation were already
> > fixed.
> > Alan, could you perhaps take a look at this and see if this is something
> > that is fixable? Thanks in advance.
> Alan, any chance you could look into the below cases? Thanks in advance.
Yes, I'll do this. Sorry I never replied to your post in 2020. I
started looking at the bug then, but somehow didn't get very far with it.
Maybe it was because there were so many individual glitches in the
report. Thanks for distilling the cases which still remain unfixed.
[ .... ]
--
Alan Mackenzie (Nuremberg, Germany).
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Tue, 11 Mar 2025 20:24:03 GMT)
Full text and
rfc822 format available.
Message #39 received at 19867 <at> debbugs.gnu.org (full text, mbox):
Alan Mackenzie <acm <at> muc.de> writes:
> Yes, I'll do this. Sorry I never replied to your post in 2020. I
> started looking at the bug then, but somehow didn't get very far with it.
> Maybe it was because there were so many individual glitches in the
> report. Thanks for distilling the cases which still remain unfixed.
Thanks!
Reply sent
to
Alan Mackenzie <acm <at> muc.de>
:
You have taken responsibility.
(Thu, 27 Mar 2025 10:43:03 GMT)
Full text and
rfc822 format available.
Notification sent
to
turner25 <at> gmail.com
:
bug acknowledged by developer.
(Thu, 27 Mar 2025 10:43:04 GMT)
Full text and
rfc822 format available.
Message #44 received at 19867-done <at> debbugs.gnu.org (full text, mbox):
Hello, Stefan.
On Tue, Mar 11, 2025 at 16:23:13 -0400, Stefan Kangas wrote:
> Alan Mackenzie <acm <at> muc.de> writes:
> > Yes, I'll do this. Sorry I never replied to your post in 2020. I
> > started looking at the bug then, but somehow didn't get very far with it.
> > Maybe it was because there were so many individual glitches in the
> > report. Thanks for distilling the cases which still remain unfixed.
> Thanks!
I've just committed a fix (a ~700 line patch ;-), so I think this bug is
finally solved.
I'm closing the bug with this post.
--
Alan Mackenzie (Nuremberg, Germany).
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Thu, 10 Apr 2025 00:55:01 GMT)
Full text and
rfc822 format available.
Message #47 received at 19867 <at> debbugs.gnu.org (full text, mbox):
Alan Mackenzie <acm <at> muc.de> writes:
> I've just committed a fix (a ~700 line patch ;-), so I think this bug is
> finally solved.
Thank you!
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Thu, 17 Apr 2025 13:44:02 GMT)
Full text and
rfc822 format available.
Message #50 received at 19867 <at> debbugs.gnu.org (full text, mbox):
Thank you for fixing this. A minor oversight in the committed patch, in cc-align.el:
351 (skip-syntax-backward " \t([{" (c-point 'boi))
This call should probably use `skip-char-backward`, because `\t`, `[`, and `{` aren't syntax chars.
It currently works by accident because of the ` ` and `(` syntax classes.
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Mon, 21 Apr 2025 20:36:01 GMT)
Full text and
rfc822 format available.
Message #53 received at 19867 <at> debbugs.gnu.org (full text, mbox):
Hello, Mattias.
On Thu, Apr 17, 2025 at 15:42:59 +0200, Mattias Engdegård wrote:
> Thank you for fixing this. A minor oversight in the committed patch, in cc-align.el:
> 351 (skip-syntax-backward " \t([{" (c-point 'boi))
> This call should probably use `skip-char-backward`, because `\t`, `[`, and `{` aren't syntax chars.
> It currently works by accident because of the ` ` and `(` syntax classes.
Thanks for spotting this and telling me about it. I'll get around to
fixing it in the next few days.
--
Alan Mackenzie (Nuremberg, Germany).
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Wed, 30 Apr 2025 18:59:02 GMT)
Full text and
rfc822 format available.
Message #56 received at 19867 <at> debbugs.gnu.org (full text, mbox):
Hello again, Mattias.
On Mon, Apr 21, 2025 at 20:35:22 +0000, Alan Mackenzie wrote:
> On Thu, Apr 17, 2025 at 15:42:59 +0200, Mattias Engdegård wrote:
> > Thank you for fixing this. A minor oversight in the committed patch, in cc-align.el:
> > 351 (skip-syntax-backward " \t([{" (c-point 'boi))
> > This call should probably use `skip-char-backward`, because `\t`, `[`, and `{` aren't syntax chars.
> > It currently works by accident because of the ` ` and `(` syntax classes.
> Thanks for spotting this and telling me about it. I'll get around to
> fixing it in the next few days.
DONE.
--
Alan Mackenzie (Nuremberg, Germany).
Information forwarded
to
bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
:
bug#19867
; Package
emacs,cc-mode
.
(Thu, 15 May 2025 18:44:02 GMT)
Full text and
rfc822 format available.
Message #59 received at 19867 <at> debbugs.gnu.org (full text, mbox):
30 apr. 2025 kl. 20.58 skrev Alan Mackenzie <acm <at> muc.de>:
> DONE.
Thank you!
bug archived.
Request was from
Debbugs Internal Request <help-debbugs <at> gnu.org>
to
internal_control <at> debbugs.gnu.org
.
(Fri, 13 Jun 2025 11:24:08 GMT)
Full text and
rfc822 format available.
This bug report was last modified 2 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.