Skip to content

Commit

Permalink
[clang-format] Fix option BreakBinaryOperations for operator >> (…
Browse files Browse the repository at this point in the history
…#122282)

Fixes #106228.
  • Loading branch information
andergnet authored Jan 18, 2025
1 parent b62e558 commit e240261
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ static bool startsNextOperand(const FormatToken &Current) {
static bool mustBreakBinaryOperation(const FormatToken &Current,
const FormatStyle &Style) {
return Style.BreakBinaryOperations != FormatStyle::BBO_Never &&
Current.CanBreakBefore &&
(Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None
? startsNextOperand
: isAlignableBinaryOperator)(Current);
Expand Down
33 changes: 33 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27976,6 +27976,11 @@ TEST_F(FormatTest, BreakBinaryOperations) {
" operand1 + operand2 - (operand3 + operand4);",
Style);

// Check operator>> special case.
verifyFormat("std::cin >> longOperand_1 >> longOperand_2 >>\n"
" longOperand_3_;",
Style);

Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;

// Logical operations
Expand Down Expand Up @@ -28054,6 +28059,13 @@ TEST_F(FormatTest, BreakBinaryOperations) {
" operand6->member;",
Style);

// Check operator>> special case.
verifyFormat("std::cin >>\n"
" longOperand_1 >>\n"
" longOperand_2 >>\n"
" longOperand_3_;",
Style);

Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
verifyFormat("result = op1 + op2 * op3 - op4;", Style);

Expand All @@ -28079,6 +28091,13 @@ TEST_F(FormatTest, BreakBinaryOperations) {
" byte_buffer[3] << 24;",
Style);

// Check operator>> special case.
verifyFormat("std::cin >>\n"
" longOperand_1 >>\n"
" longOperand_2 >>\n"
" longOperand_3_;",
Style);

Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;

Expand Down Expand Up @@ -28153,6 +28172,13 @@ TEST_F(FormatTest, BreakBinaryOperations) {
" << 24;",
Style);

// Check operator>> special case.
verifyFormat("std::cin\n"
" >> longOperand_1\n"
" >> longOperand_2\n"
" >> longOperand_3_;",
Style);

Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
verifyFormat("result = op1 + op2 * op3 - op4;", Style);

Expand All @@ -28177,6 +28203,13 @@ TEST_F(FormatTest, BreakBinaryOperations) {
" | byte_buffer[2] << 16\n"
" | byte_buffer[3] << 24;",
Style);

// Check operator>> special case.
verifyFormat("std::cin\n"
" >> longOperand_1\n"
" >> longOperand_2\n"
" >> longOperand_3_;",
Style);
}

TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) {
Expand Down

0 comments on commit e240261

Please sign in to comment.