[clang] [clang-format] Make EndsInComma in ContinuationIndenter consistent (PR #146256)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 28 18:18:30 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/146256
None
>From b7dd2cee0b6064f29e1f9562d26675152bb2756b Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 28 Jun 2025 18:13:43 -0700
Subject: [PATCH] [clang-format] Make EndsInComma in ContinuationIndenter
consistent
---
clang/lib/Format/ContinuationIndenter.cpp | 17 +++++++++--------
clang/unittests/Format/FormatTest.cpp | 14 --------------
2 files changed, 9 insertions(+), 22 deletions(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index b4745477b96ef..4010f7fbd25be 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1929,6 +1929,15 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
return;
}
+ const bool EndsInComma = [](const FormatToken *Tok) {
+ if (!Tok)
+ return false;
+ const auto *Prev = Tok->getPreviousNonComment();
+ if (!Prev)
+ return false;
+ return Prev->is(tok::comma);
+ }(Current.MatchingParen);
+
unsigned NewIndent;
unsigned LastSpace = CurrentState.LastSpace;
bool AvoidBinPacking;
@@ -1948,9 +1957,6 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
NewIndent = CurrentState.LastSpace + Style.ContinuationIndentWidth;
}
const FormatToken *NextNonComment = Current.getNextNonComment();
- bool EndsInComma = Current.MatchingParen &&
- Current.MatchingParen->Previous &&
- Current.MatchingParen->Previous->is(tok::comma);
AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) ||
Style.isProto() || !Style.BinPackArguments ||
(NextNonComment && NextNonComment->isOneOf(
@@ -1984,11 +1990,6 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
LastSpace = std::max(LastSpace, CurrentState.Indent);
}
- bool EndsInComma =
- Current.MatchingParen &&
- Current.MatchingParen->getPreviousNonComment() &&
- Current.MatchingParen->getPreviousNonComment()->is(tok::comma);
-
// If ObjCBinPackProtocolList is unspecified, fall back to BinPackParameters
// for backwards compatibility.
bool ObjCBinPackProtocolList =
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a05bf8305716b..944e7c3fb152a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27953,13 +27953,6 @@ TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
"};",
Style);
verifyFormat("void foo() {\n"
- " auto thing = test{\n"
- " {\n"
- " {13}, {something}, // A\n"
- " }\n"
- " };\n"
- "}",
- "void foo() {\n"
" auto thing = test{\n"
" {\n"
" {13},\n"
@@ -28017,13 +28010,6 @@ TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
"};",
Style);
verifyFormat("void foo() {\n"
- " auto thing = test{\n"
- " {\n"
- " {13}, {something}, // A\n"
- " }\n"
- " };\n"
- "}",
- "void foo() {\n"
" auto thing = test{\n"
" {\n"
" {13},\n"
More information about the cfe-commits
mailing list