Skip to content

Commit e240261

Browse files
authored
[clang-format] Fix option BreakBinaryOperations for operator >> (#122282)
Fixes #106228.
1 parent b62e558 commit e240261

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ static bool startsNextOperand(const FormatToken &Current) {
148148
static bool mustBreakBinaryOperation(const FormatToken &Current,
149149
const FormatStyle &Style) {
150150
return Style.BreakBinaryOperations != FormatStyle::BBO_Never &&
151+
Current.CanBreakBefore &&
151152
(Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None
152153
? startsNextOperand
153154
: isAlignableBinaryOperator)(Current);

clang/unittests/Format/FormatTest.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27976,6 +27976,11 @@ TEST_F(FormatTest, BreakBinaryOperations) {
2797627976
" operand1 + operand2 - (operand3 + operand4);",
2797727977
Style);
2797827978

27979+
// Check operator>> special case.
27980+
verifyFormat("std::cin >> longOperand_1 >> longOperand_2 >>\n"
27981+
" longOperand_3_;",
27982+
Style);
27983+
2797927984
Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
2798027985

2798127986
// Logical operations
@@ -28054,6 +28059,13 @@ TEST_F(FormatTest, BreakBinaryOperations) {
2805428059
" operand6->member;",
2805528060
Style);
2805628061

28062+
// Check operator>> special case.
28063+
verifyFormat("std::cin >>\n"
28064+
" longOperand_1 >>\n"
28065+
" longOperand_2 >>\n"
28066+
" longOperand_3_;",
28067+
Style);
28068+
2805728069
Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
2805828070
verifyFormat("result = op1 + op2 * op3 - op4;", Style);
2805928071

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

28094+
// Check operator>> special case.
28095+
verifyFormat("std::cin >>\n"
28096+
" longOperand_1 >>\n"
28097+
" longOperand_2 >>\n"
28098+
" longOperand_3_;",
28099+
Style);
28100+
2808228101
Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
2808328102
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2808428103

@@ -28153,6 +28172,13 @@ TEST_F(FormatTest, BreakBinaryOperations) {
2815328172
" << 24;",
2815428173
Style);
2815528174

28175+
// Check operator>> special case.
28176+
verifyFormat("std::cin\n"
28177+
" >> longOperand_1\n"
28178+
" >> longOperand_2\n"
28179+
" >> longOperand_3_;",
28180+
Style);
28181+
2815628182
Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
2815728183
verifyFormat("result = op1 + op2 * op3 - op4;", Style);
2815828184

@@ -28177,6 +28203,13 @@ TEST_F(FormatTest, BreakBinaryOperations) {
2817728203
" | byte_buffer[2] << 16\n"
2817828204
" | byte_buffer[3] << 24;",
2817928205
Style);
28206+
28207+
// Check operator>> special case.
28208+
verifyFormat("std::cin\n"
28209+
" >> longOperand_1\n"
28210+
" >> longOperand_2\n"
28211+
" >> longOperand_3_;",
28212+
Style);
2818028213
}
2818128214

2818228215
TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) {

0 commit comments

Comments
 (0)