Skip to content

[C] Alignment of nested structure/array initializers #1293

Open

Description

@stanczew

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 }
};

Attachments

original.c
expected.c
actual.c
debug.txt

Activity

  1. guy-maurel commented on Dec 1, 2017

    @guy-maurel
    Contributor

    related to #1349

  2. guy-maurel commented on Mar 2, 2018

    @guy-maurel
    Contributor

    Let me change the little example to:

    struct record r = {
            .id = 55555555555555,
            .num = { 111, 22222, 3333333 },
    	.data = { 0 }
    };
    

    The output is now:

    struct record r = {
            .id   = 55555555555555,
            .num  = { 111,         22222,3333333 },
            .data = { 0 }
    };
    

    I see two questions:

    • the alignment of the assign-sign
    • the alignment of the values.
      Let us begin with the first: should we align the assign-sign ?
  3. stanczew commented on Mar 4, 2018

    @stanczew
    Author

    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:

    struct record rs[] = {
    	[SOME_ENUM] = { 1, 2, 3 },
    	[ANOTHER_ENUM] = { 111, 222, 333 },
    };
    

    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.

  4. guy-maurel commented on Mar 4, 2018

    @guy-maurel
    Contributor

    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.

  5. guy-maurel commented on Jun 4, 2018

    @guy-maurel
    Contributor

    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

  6. guy-maurel commented on Apr 4, 2019

    @guy-maurel
    Contributor

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions