This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Add support of consecutive declarations alignment
ClosedPublic

Authored by berenm on Aug 26 2015, 7:45 AM.

Details

Reviewers
djasper
Summary

This allows clang-format to align identifiers in consecutive declarations.

This is, arguably, a feature useful for increasing the readability of the code,
in the same way the alignment of assignations is. It is also present in other
tools such as uncrustify for example.

The code is a slightly modified version of the consecutive assignment
alignment code. Currently only the identifiers are aligned, and there is no
support of alignment of the pointer star or reference symbol.

Diff Detail

Event Timeline

berenm updated this revision to Diff 33197.Aug 26 2015, 7:45 AM
berenm retitled this revision from to [clang-format] Add support of consecutive declarations alignment.
berenm updated this object.
berenm added a reviewer: djasper.
berenm added a subscriber: cfe-commits.
djasper added inline comments.Aug 26 2015, 8:07 AM
unittests/Format/FormatTest.cpp
8619

This needs tests that check what happens if both declarations and assignments are aligned.

8703

Reduce Alignment.ColumnLimit so that tests don't need line-wraps.

Actually, I think there is a bug in the assignment alignment code. Even without this patch, this code doesn't align properly:

int oneTwoThree = 123;
int oneTwo      = 12;
method();

I don't think I completely understand the assignment alignment code, and that's why mine is slightly rewritten. I will open another request for fixes in the assignment alignment code.

unittests/Format/FormatTest.cpp
8619

Indeed, I will add such tests.

berenm updated this revision to Diff 33212.Aug 26 2015, 9:32 AM

Added some test cases in combined usage with AlignConsecutiveAssignments.

The tests should pass with http://reviews.llvm.org/D12369 applied.

Ping?

The unit tests should work fine, now that D12369 has been merged.

djasper added inline comments.Sep 29 2015, 8:10 AM
lib/Format/WhitespaceManager.cpp
270

Generally, we don't use braces for single-statement-ifs in LLVM style.

unittests/Format/FormatTest.cpp
8699

Can you add a case (unless I missed it) where aligning both consecutive assignments and consecutive declarations exceed the column limit? What should happen in that case? I am thinking of something like:

int loooooooooooooooongName = 1;
LoooooooooooongType i = bbbbbbbbbbbbbbbbbbbbbbb;
berenm added inline comments.Sep 29 2015, 9:09 AM
unittests/Format/FormatTest.cpp
8699

AFAIR, the alignment doesn't work very well with the column limit at the moment. This is already true wrt the assignment alignment. The column limit is enforced before the alignment is done and aligning variable names and / or assignment will expand beyond that limit.

I will add the test case but I haven't tried to fix this issue yet.

Should test cases check the current behaviour or the ideal expected behaviour (that doesn't work) ?

djasper added inline comments.Sep 29 2015, 9:31 AM
unittests/Format/FormatTest.cpp
8699

Ah interesting. Then I am ok with submitting this as is. However, the issue should be fixed for both. It is fine to do the alignment after doing the overall layout, but we should then simply not align if alignment would violate the column limit. This is already done for trailing comments.

Please add a comment (FIXME) about this somewhere.

berenm updated this revision to Diff 36206.Oct 1 2015, 2:37 AM
  • Add support of ColumnLimit restrition in AlignConsecutiveAssignments and AlignConsecutiveDeclarations.
berenm updated this revision to Diff 36207.Oct 1 2015, 2:39 AM

Fix arcanist insisting on creating the diff against old revision...

berenm marked 7 inline comments as done.Oct 1 2015, 2:43 AM
berenm added inline comments.
unittests/Format/FormatTest.cpp
8699

I think I actually managed to do something about that.

I added a test case as well to check that AlignConsecutive* respect the column limit.

berenm marked 2 inline comments as done.Oct 1 2015, 2:43 AM
djasper accepted this revision.Oct 1 2015, 2:52 AM
djasper edited edge metadata.

This looks awesome. Do you have commit access?

unittests/Format/FormatTest.cpp
8699

Very nice!

This revision is now accepted and ready to land.Oct 1 2015, 2:52 AM
berenm added a comment.Oct 1 2015, 2:56 AM

Thanks, I don't have commit access.

djasper closed this revision.Oct 1 2015, 3:08 AM

Submitted as r248999. Thank you!