Skip to content

[clang-format] Fix option BreakBinaryOperations for operator >> #122282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -27987,6 +27987,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 @@ -28065,6 +28070,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 @@ -28090,6 +28102,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 @@ -28164,6 +28183,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 @@ -28188,6 +28214,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
Loading