Skip to content

Commit b12fffd

Browse files
owencazahiraam
authored andcommitted
[clang-format] Fix a bug in ContinuationIndenter (llvm#66354)
See https://reviews.llvm.org/D136154#3890747 for context. Fixes part of llvm#58592.
1 parent f5d9d92 commit b12fffd

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -876,15 +876,15 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
876876
FormatStyle::BCIS_AfterColon) {
877877
CurrentState.Indent = State.Column;
878878
CurrentState.LastSpace = State.Column;
879-
} else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
880-
TT_CtorInitializerColon)) &&
879+
} else if (Previous.isOneOf(TT_ConditionalExpr, TT_CtorInitializerColon)) {
880+
CurrentState.LastSpace = State.Column;
881+
} else if (Previous.is(TT_BinaryOperator) &&
881882
((Previous.getPrecedence() != prec::Assignment &&
882883
(Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
883884
Previous.NextOperator)) ||
884885
Current.StartsBinaryExpression)) {
885886
// Indent relative to the RHS of the expression unless this is a simple
886-
// assignment without binary expression on the RHS. Also indent relative to
887-
// unary operators and the colons of constructor initializers.
887+
// assignment without binary expression on the RHS.
888888
if (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None)
889889
CurrentState.LastSpace = State.Column;
890890
} else if (Previous.is(TT_InheritanceColon)) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7108,6 +7108,29 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
71087108
"(someOtherLongishConditionPart1 || "
71097109
"someOtherEvenLongerNestedConditionPart2);",
71107110
Style);
7111+
7112+
Style = getLLVMStyleWithColumns(20);
7113+
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7114+
Style.BinPackParameters = false;
7115+
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7116+
Style.ContinuationIndentWidth = 2;
7117+
verifyFormat("struct Foo {\n"
7118+
" Foo(\n"
7119+
" int arg1,\n"
7120+
" int arg2)\n"
7121+
" : Base(\n"
7122+
" arg1,\n"
7123+
" arg2) {}\n"
7124+
"};",
7125+
Style);
7126+
verifyFormat("return abc\n"
7127+
" ? foo(\n"
7128+
" a,\n"
7129+
" b,\n"
7130+
" bar(\n"
7131+
" abc))\n"
7132+
" : g(abc);",
7133+
Style);
71117134
}
71127135

71137136
TEST_F(FormatTest, ExpressionIndentationStrictAlign) {

0 commit comments

Comments
 (0)