[clang] [clang-format] Allow array alignment on non-rectangular arrays (PR #143781)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 17 09:01:58 PDT 2025
================
@@ -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)
+ 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 }
+ //
----------------
HazardyKnusperkeks wrote:
Maybe put this next to each other, to not waste as much vertical space.
https://github.com/llvm/llvm-project/pull/143781
More information about the cfe-commits
mailing list