Skip to content

Commit c6b8b6c

Browse files
owencaAlexisPerry
authored andcommitted
[clang-format] Handle function try block with ctor-initializer (llvm#95878)
Fixes llvm#58987. Fixes llvm#95679.
1 parent 65eff54 commit c6b8b6c

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,9 +2955,15 @@ void UnwrappedLineParser::parseTryCatch() {
29552955
assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
29562956
nextToken();
29572957
bool NeedsUnwrappedLine = false;
2958+
bool HasCtorInitializer = false;
29582959
if (FormatTok->is(tok::colon)) {
2960+
auto *Colon = FormatTok;
29592961
// We are in a function try block, what comes is an initializer list.
29602962
nextToken();
2963+
if (FormatTok->is(tok::identifier)) {
2964+
HasCtorInitializer = true;
2965+
Colon->setFinalizedType(TT_CtorInitializerColon);
2966+
}
29612967

29622968
// In case identifiers were removed by clang-tidy, what might follow is
29632969
// multiple commas in sequence - before the first identifier.
@@ -2966,14 +2972,11 @@ void UnwrappedLineParser::parseTryCatch() {
29662972

29672973
while (FormatTok->is(tok::identifier)) {
29682974
nextToken();
2969-
if (FormatTok->is(tok::l_paren))
2975+
if (FormatTok->is(tok::l_paren)) {
29702976
parseParens();
2971-
if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) &&
2972-
FormatTok->is(tok::l_brace)) {
2973-
do {
2974-
nextToken();
2975-
} while (FormatTok->isNot(tok::r_brace));
2977+
} else if (FormatTok->is(tok::l_brace)) {
29762978
nextToken();
2979+
parseBracedList();
29772980
}
29782981

29792982
// In case identifiers were removed by clang-tidy, what might follow is
@@ -2989,6 +2992,8 @@ void UnwrappedLineParser::parseTryCatch() {
29892992
keepAncestorBraces();
29902993

29912994
if (FormatTok->is(tok::l_brace)) {
2995+
if (HasCtorInitializer)
2996+
FormatTok->setFinalizedType(TT_FunctionLBrace);
29922997
CompoundStatementIndenter Indenter(this, Style, Line->Level);
29932998
parseBlock();
29942999
if (Style.BraceWrapping.BeforeCatch)

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,6 +3164,22 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
31643164
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
31653165
}
31663166

3167+
TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
3168+
auto Tokens =
3169+
annotate("Foo::Foo(int x, int y) try\n"
3170+
" : foo{[] -> std::string { return {}; }(), x}, bar{y} {\n"
3171+
"} catch (...) {\n"
3172+
"}");
3173+
ASSERT_EQ(Tokens.size(), 45u);
3174+
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_CtorDtorDeclName);
3175+
EXPECT_TOKEN(Tokens[11], tok::colon, TT_CtorInitializerColon);
3176+
EXPECT_TOKEN(Tokens[14], tok::l_square, TT_LambdaLSquare);
3177+
EXPECT_TOKEN(Tokens[16], tok::arrow, TT_TrailingReturnArrow);
3178+
EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_LambdaLBrace);
3179+
EXPECT_TOKEN(Tokens[31], tok::comma, TT_CtorInitializerComma);
3180+
EXPECT_TOKEN(Tokens[36], tok::l_brace, TT_FunctionLBrace);
3181+
}
3182+
31673183
} // namespace
31683184
} // namespace format
31693185
} // namespace clang

0 commit comments

Comments
 (0)