Skip to content

Commit

Permalink
[clang-format] Fix a bug in ContinuationIndenter (llvm#66354)
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca authored and zahiraam committed Oct 24, 2023
1 parent 6df9bb1 commit 24576f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
8 changes: 4 additions & 4 deletions clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -876,15 +876,15 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
FormatStyle::BCIS_AfterColon) {
CurrentState.Indent = State.Column;
CurrentState.LastSpace = State.Column;
} else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
TT_CtorInitializerColon)) &&
} else if (Previous.isOneOf(TT_ConditionalExpr, TT_CtorInitializerColon)) {
CurrentState.LastSpace = State.Column;
} else if (Previous.is(TT_BinaryOperator) &&
((Previous.getPrecedence() != prec::Assignment &&
(Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
Previous.NextOperator)) ||
Current.StartsBinaryExpression)) {
// Indent relative to the RHS of the expression unless this is a simple
// assignment without binary expression on the RHS. Also indent relative to
// unary operators and the colons of constructor initializers.
// assignment without binary expression on the RHS.
if (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None)
CurrentState.LastSpace = State.Column;
} else if (Previous.is(TT_InheritanceColon)) {
Expand Down
23 changes: 23 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -7108,6 +7108,29 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
"(someOtherLongishConditionPart1 || "
"someOtherEvenLongerNestedConditionPart2);",
Style);

Style = getLLVMStyleWithColumns(20);
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
Style.BinPackParameters = false;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
Style.ContinuationIndentWidth = 2;
verifyFormat("struct Foo {\n"
" Foo(\n"
" int arg1,\n"
" int arg2)\n"
" : Base(\n"
" arg1,\n"
" arg2) {}\n"
"};",
Style);
verifyFormat("return abc\n"
" ? foo(\n"
" a,\n"
" b,\n"
" bar(\n"
" abc))\n"
" : g(abc);",
Style);
}

TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
Expand Down

0 comments on commit 24576f2

Please sign in to comment.