Skip to content

Commit 4bb6856

Browse files
committed
[clang-format] Correctly compute SplitPenalty of TrailingReturnArrow
Fixes llvm#105480.
1 parent 3496245 commit 4bb6856

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4050,7 +4050,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
40504050
ChildSize + Current->SpacesRequiredBefore;
40514051
}
40524052

4053-
if (Current->is(TT_CtorInitializerColon))
4053+
if (Current->isOneOf(TT_CtorInitializerColon, TT_LambdaLSquare))
40544054
InFunctionDecl = false;
40554055

40564056
// FIXME: Only calculate this if CanBreakBefore is true once static
@@ -4211,7 +4211,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
42114211
}
42124212
if (Right.is(TT_PointerOrReference))
42134213
return 190;
4214-
if (Right.is(TT_TrailingReturnArrow))
4214+
if (!InFunctionDecl && Right.is(TT_TrailingReturnArrow))
42154215
return 110;
42164216
if (Left.is(tok::equal) && Right.is(tok::l_brace))
42174217
return 160;

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class TokenAnnotatorTest : public testing::Test {
4242
EXPECT_EQ((FormatTok)->getPrecedence(), Prec) << *(FormatTok)
4343
#define EXPECT_BRACE_KIND(FormatTok, Kind) \
4444
EXPECT_EQ(FormatTok->getBlockKind(), Kind) << *(FormatTok)
45+
#define EXPECT_SPLIT_PENALTY(FormatTok, Penalty) \
46+
EXPECT_EQ(FormatTok->SplitPenalty, (unsigned)(Penalty)) << *(FormatTok)
4547
#define EXPECT_TOKEN(FormatTok, Kind, Type) \
4648
do { \
4749
EXPECT_TOKEN_KIND(FormatTok, Kind); \
@@ -3369,6 +3371,20 @@ TEST_F(TokenAnnotatorTest, GNULanguageStandard) {
33693371
EXPECT_TOKEN(Tokens[2], tok::spaceship, TT_BinaryOperator);
33703372
}
33713373

3374+
TEST_F(TokenAnnotatorTest, SplitPenalty) {
3375+
auto Style = getLLVMStyle();
3376+
Style.ColumnLimit = 20;
3377+
3378+
auto Tokens = annotate("class foo {\n"
3379+
" auto bar()\n"
3380+
" -> bool;\n"
3381+
"};",
3382+
Style);
3383+
ASSERT_EQ(Tokens.size(), 13u);
3384+
EXPECT_TOKEN(Tokens[7], tok::arrow, TT_TrailingReturnArrow);
3385+
EXPECT_SPLIT_PENALTY(Tokens[7], 23);
3386+
}
3387+
33723388
} // namespace
33733389
} // namespace format
33743390
} // namespace clang

0 commit comments

Comments
 (0)