[llvm-dev] Clang-format: Allow alignment across empty lines and comments

Lukas Barth via llvm-dev llvm-dev at lists.llvm.org
Fri Jan 1 06:08:23 PST 2021


Hi,

I would like to patch clang-format such that its alignment tools (such
as AlignConsecutiveAssignments) can work across empty lines and
comments. In effect, I would like that this code:

```
int foo = 1;
int longbar = 2;

/* Some comment */
int baz = 3;
```

can automatically be formatted to this:

```
int foo     = 1;
int longbar = 2;

/* Some comment */
int baz     = 3;
```

I have a first working patch, but since I've never contributed to LLVM, I
wanted to discuss here (a) whether this change would be accepted in
principle and (b) how the configuration options should look like, before
I put in the work to make the patch "production ready".

Regarding (a): I think this would be useful. People use this style in
practice [1]. Also, if you have a long block of aligned assignments, and some
code change (temporarily, maybe) comments-out a single assignment in the
middle, this suddenly causes clang-format to re-indent the assignments
below that - even though there is no functional change to those
assignments.

Regarding (b): Currently, the `AlignConsecutive{Assignments, BitFields,
Declarations, Macros}` are boolean options. I see two ways of
controlling intended behavior: Either, add two new booleans per option,
in the style of `AlignConsecutiveAssignmentsAcrossEmptyLines` and
`AlignConsecutiveAssignmentsAcrossComments`, or make the four existing
options enums, with the choices
       - `<Something>_None` (the old `false`)
       - `<Something>_Consecutive` (the old `true`)
       - `<Something>_AcrossComments`
       - `<Something>_AcrossEmptyLines`
       - `<Something>_AcrossEmptyLinesAndComments`

Of course I would parse `true` to `Consecutive` and `false` to `None`
for backwards compatibility. I favor this enum-approach.


One final question: I have a patch for the `AlignConsecutiveAssignments`
alignment. The others would be very similar, but some busy-work
(changing unit tests, etc.) is required. Would it be preferrable if I
first submitted the smaller patch for AlignConsecutiveAssignments,
discuss it and get it in, and then supply the patch for the other four,
or would a one-in-all patch be preferred?


Regards,
Lukas


[1] https://github.com/djcb/mu/blob/46bd705131db50dbebe624f0fe513fe615382582/lib/mu-msg.h#L40-L48
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 861 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20210101/b6106d8c/attachment.sig>


More information about the llvm-dev mailing list