Repository navigation
Description
Environment
Uncrustify version: Uncrustify-0.65-106-95188777
Operating systems: Linux, Windows
Config
align_struct_init_span = 1
Input
struct container c = {
.record_array = {
{
.id = 3,
.num = { 123123123, 2525, 3 },
.data = { 0 }
},
{
.id = 5,
.num = { 1, 2, 3 },
.data = { 0 }
}
}
};
Expected output
struct container c = {
.record_array = {
{
.id = 3,
.num = { 123123123, 2525, 3 },
.data = { 0 }
},
{
.id = 5,
.num = { 1, 2, 3 },
.data = { 0 }
}
}
};
Actual output
struct container c = {
.record_array = {
{
.id = 3,
.num ={ 123123123, 2525, 3 },
.data ={ 0 }
},
{
.id = 5,
.num ={ 1, 2, 3 },
.data ={ 0 }
}
}
};
Description
I am using align_struct_init_span to have array-of-structures initializers like such:
struct num n[] = {
{ 1, 2, 3 },
{ 111, 222, 333 }
};
get aligned like that:
struct num n[] = {
{ 1, 2, 3 },
{ 111, 222, 333 }
};
which Uncrustify does without issues.
However, initializers with nested structures get formatted weirdly: elements of nested structures with different parents get aligned with each other, additional spacing is added inside the nested structs, and spaces before braces are removed.
Workarounds
I can get the desired behavior by using align_assign_span instead of align_struct_init_span, however then the array-of-structures example above no longer gets aligned (and also I don't want to align any other assignments). Using both align_struct_init_span and align_assign_span helps a little bit, but the result is still far from ideal:
struct container c = {
.record_array = {
{
.id = 3,
.num = { 123123123, 2525, 3 },
.data ={ 0 }
},
{
.id = 5,
.num = { 1, 2, 3 },
.data ={ 0 }
}
}
};
Is there maybe some combination of configuration options that I'm missing? I've been trying various combinations of alignment-related options, without success (= both of examples above getting aligned properly).
Additional info
Parts of the issue can be observed for simpler initializers. Example:
struct record r = {
.id = 5,
.num = { 1, 2, 3 },
.data = { 0 }
};
gets formatted to:
struct record r = {
.id = 5,
.num = { 1,2, 3 },
.data = { 0 }
};
Activity
guy-maurel commentedon Dec 1, 2017
related to #1349
guy-maurel commentedon Mar 2, 2018
Let me change the little example to:
The output is now:
I see two questions:
Let us begin with the first: should we align the assign-sign ?
stanczew commentedon Mar 4, 2018
With regards to this specific example, I think both options (either aligning the assign-sign or not) are acceptable. My expected version has the alignment, and I think it is more consistent this way, but I can see the argument for the other way, too.
However, this changes a bit for array initializers with designators:
In this case I think not aligning the assign-sign would look worse, especially if we would then like to align the values. The reason for this is that all array elements are of the same type, and can be expected to be written in the same way; this is not true for structure initializers, where structure fields can be of different types.
So in the end I think the assign-sign should be aligned, but mainly for consistency with the array example above.
guy-maurel commentedon Mar 4, 2018
The problem is having a struct initializer inside a struct initializer inside something.
uncrustify can't yet deal with that.
The output are not predictable.
guy-maurel commentedon Jun 4, 2018
The problem is having a struct initializer inside a struct initializer inside something.
uncrustify can't yet deal with that.
The output are not predictable.
Related to #1349, #1601, #1727
guy-maurel commentedon Apr 4, 2019
The problem is having a struct initializer inside a struct initializer inside something.
uncrustify can't yet deal with that.
The output are not predictable.
Related to #967, #1349, #1601, #1727, #1848