-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang-format] Allow array alignment on non-rectangular arrays #143781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -4227,13 +4227,17 @@ void TokenAnnotator::calculateArrayInitializerColumnList( | |||||||
if (Line.First == Line.Last) | ||||||||
return; | ||||||||
auto *CurrentToken = Line.First; | ||||||||
CurrentToken->ArrayInitializerLineStart = true; | ||||||||
unsigned Depth = 0; | ||||||||
while (CurrentToken && CurrentToken != Line.Last) { | ||||||||
if (CurrentToken->is(tok::l_brace)) { | ||||||||
CurrentToken->IsArrayInitializer = true; | ||||||||
if (CurrentToken->Next) | ||||||||
CurrentToken->Next->MustBreakBefore = true; | ||||||||
|
||||||||
// Ensure the end brace of the outer array is on its own line | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
if (CurrentToken->MatchingParen) | ||||||||
CurrentToken->MatchingParen->MustBreakBefore = true; | ||||||||
|
||||||||
CurrentToken = | ||||||||
calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1); | ||||||||
} else { | ||||||||
|
@@ -4249,11 +4253,40 @@ FormatToken *TokenAnnotator::calculateInitializerColumnList( | |||||||
++Depth; | ||||||||
else if (CurrentToken->is(tok::r_brace)) | ||||||||
--Depth; | ||||||||
|
||||||||
// Ensure each outer array element starts on its own line | ||||||||
if (Depth == 1 && CurrentToken->is(tok::comma)) { | ||||||||
auto *NextNonComment = CurrentToken->getNextNonComment(); | ||||||||
if (NextNonComment) | ||||||||
Comment on lines
+4259
to
+4260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
NextNonComment->MustBreakBefore = true; | ||||||||
} | ||||||||
|
||||||||
if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) { | ||||||||
CurrentToken = CurrentToken->Next; | ||||||||
if (!CurrentToken) | ||||||||
break; | ||||||||
CurrentToken->StartsColumn = true; | ||||||||
|
||||||||
// Right (closing) braces should not count as starting a column because | ||||||||
// they are aligned using separate logic. | ||||||||
|
||||||||
// Note: This uses startsSequence() so that trailing comments are skipped | ||||||||
// when checking if the token after a comma/l-brace is a r_brace. We can't | ||||||||
// just ignore comments in general, because an inline comment with | ||||||||
// something else after it should still count as starting a column. | ||||||||
// IE: | ||||||||
// | ||||||||
// { // a | ||||||||
// 4 | ||||||||
// } | ||||||||
// | ||||||||
// vs. | ||||||||
// | ||||||||
// { /* a */ 4 } | ||||||||
// | ||||||||
Comment on lines
+4277
to
+4285
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe put this next to each other, to not waste as much vertical space. |
||||||||
// In the first case, the comment does not start a column, but in the | ||||||||
// second it does. | ||||||||
CurrentToken->StartsColumn = !CurrentToken->startsSequence(tok::r_brace); | ||||||||
|
||||||||
CurrentToken = CurrentToken->Previous; | ||||||||
} | ||||||||
CurrentToken = CurrentToken->Next; | ||||||||
|
@@ -6199,7 +6232,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, | |||||||
// block-indented initialization list. | ||||||||
if (Right.is(tok::r_brace)) { | ||||||||
return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) || | ||||||||
(Right.isBlockIndentedInitRBrace(Style))); | ||||||||
Right.isBlockIndentedInitRBrace(Style) || | ||||||||
Right.closesAlignedArrayInitializer(Style)); | ||||||||
} | ||||||||
|
||||||||
// We only break before r_paren if we're in a block indented context. | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.