This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix alignment of preprocessor trailing comments
ClosedPublic

Authored by krasimir on Jun 7 2017, 4:37 AM.

Details

Summary

This patch is a follow-up of https://reviews.llvm.org/rL304687, which fixed an
overflow in the comment alignment code in clang-format. The token length of
trailing comments of preprocessor directives is calculated incorrectly by
including the text between consecutive directives. That causes them to not being
aligned.

For example, in this code with column limit 20

#if A
#else  // A
int iiii;
#endif // B

the length of the token // A was wrongly calculated as 14 = 5 (the size of // A\n) plus 9 (the size of int iiii;) and so // A wouldn't be aligned with // B and this was produced:

#if A
#else // A
int iiii;
#endif // B

This patch fixes this case.

Diff Detail

Repository
rL LLVM

Event Timeline

krasimir created this revision.Jun 7 2017, 4:37 AM
krasimir edited the summary of this revision. (Show Details)Jun 7 2017, 4:42 AM
krasimir added a reviewer: alexfh.
krasimir edited the summary of this revision. (Show Details)
alexfh accepted this revision.Jun 7 2017, 5:54 AM
alexfh added a subscriber: cfe-commits.

LG

lib/Format/WhitespaceManager.cpp
112 ↗(On Diff #101692)

I'd pull SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd) out to a variable.

This revision is now accepted and ready to land.Jun 7 2017, 5:55 AM
krasimir updated this revision to Diff 101734.Jun 7 2017, 7:04 AM
  • Address review comments
This revision was automatically updated to reflect the committed changes.