[cfe-dev] [clang-format] Align all struct/union members without aligning variable declarations

Cristi Anichitei via cfe-dev cfe-dev at lists.llvm.org
Wed Apr 15 08:06:01 PDT 2020


This is the first time I post on a mailing list like this so I'm sorry if
something is not formatted correctly. I tried searching for a similar
thread, but I found nothing.

Currently, clang-format is not really able to format a structure as this (I
really hope this will be displayed as I want to, but just in case:

> struct
> {
>     int     field1;
>     char    *field2;
>     struct
> } my_struct;

Almost all options format it like this:

> struct
> {
>     int field1;
>     char *field2;
> } my_struct;

This looks ok for small structs, but makes longer structs harder to read.
This is subjective, but I think that having all the fields aligned is
sometimes better.

There is a workaround that almost works, but doesn't. If you set
*AlignConsecutiveDeclarations* to *true* the fields will be aligned in the
above case. Unfortunately this will also align variable declarations, which
may not be what a user is looking for.
Also, if you have any kind of comments above the fields it will not work

Is there any possibility of having an option that aligns the fields inside
a structure?

It should probably apply to unions as well.

Maybe there is already a reason for which this wasn't included in the
options and I'm missing it?

Best regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200415/a848eb13/attachment.html>

More information about the cfe-dev mailing list