[clang] [clang-format] Handle function try block with ctor-initializer (PR #95878)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 18 18:00:55 PDT 2024
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/95878
>From 299924c9f9485e7a784ffedcb6ec4fbccf5ad6f7 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Mon, 17 Jun 2024 21:13:10 -0700
Subject: [PATCH 1/2] [clang-format] Handle function try block with
ctor-initializer
Fixes #58987.
Fixes #95679.
---
clang/lib/Format/UnwrappedLineParser.cpp | 17 +++++++++++------
clang/unittests/Format/TokenAnnotatorTest.cpp | 15 +++++++++++++++
2 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index df5bb757a99ec..ce877ac2bb9ef 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2955,9 +2955,15 @@ void UnwrappedLineParser::parseTryCatch() {
assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
nextToken();
bool NeedsUnwrappedLine = false;
+ bool HasCtorInitializer = false;
if (FormatTok->is(tok::colon)) {
+ auto *Colon = FormatTok;
// We are in a function try block, what comes is an initializer list.
nextToken();
+ if (FormatTok->is(tok::identifier)) {
+ HasCtorInitializer = true;
+ Colon->setFinalizedType(TT_CtorInitializerColon);
+ }
// In case identifiers were removed by clang-tidy, what might follow is
// multiple commas in sequence - before the first identifier.
@@ -2966,14 +2972,11 @@ void UnwrappedLineParser::parseTryCatch() {
while (FormatTok->is(tok::identifier)) {
nextToken();
- if (FormatTok->is(tok::l_paren))
+ if (FormatTok->is(tok::l_paren)) {
parseParens();
- if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) &&
- FormatTok->is(tok::l_brace)) {
- do {
- nextToken();
- } while (FormatTok->isNot(tok::r_brace));
+ } else if (FormatTok->is(tok::l_brace)) {
nextToken();
+ parseBracedList();
}
// In case identifiers were removed by clang-tidy, what might follow is
@@ -2989,6 +2992,8 @@ void UnwrappedLineParser::parseTryCatch() {
keepAncestorBraces();
if (FormatTok->is(tok::l_brace)) {
+ if (HasCtorInitializer)
+ FormatTok->setFinalizedType(TT_FunctionLBrace);
CompoundStatementIndenter Indenter(this, Style, Line->Level);
parseBlock();
if (Style.BraceWrapping.BeforeCatch)
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 3e9638d9f3c56..487582ee4e10d 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3164,6 +3164,21 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
}
+TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
+ auto Tokens =
+ annotate("Ctor::Ctor(int x, int y) try\n"
+ " : foo{[]() -> std::string { return {}; }(), x}, bar{y} {\n"
+ "} catch (...) {\n"
+ "}");
+ ASSERT_EQ(Tokens.size(), 47u);
+ EXPECT_TOKEN(Tokens[11], tok::colon, TT_CtorInitializerColon);
+ EXPECT_TOKEN(Tokens[14], tok::l_square, TT_LambdaLSquare);
+ EXPECT_TOKEN(Tokens[18], tok::arrow, TT_TrailingReturnArrow);
+ EXPECT_TOKEN(Tokens[22], tok::l_brace, TT_LambdaLBrace);
+ EXPECT_TOKEN(Tokens[33], tok::comma, TT_CtorInitializerComma);
+ EXPECT_TOKEN(Tokens[38], tok::l_brace, TT_FunctionLBrace);
+}
+
} // namespace
} // namespace format
} // namespace clang
>From 733592d7cebd0b54ad9b08b506ab971f4549d7f0 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 18 Jun 2024 17:53:51 -0700
Subject: [PATCH 2/2] Tweak the test case.
---
clang/unittests/Format/TokenAnnotatorTest.cpp | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 487582ee4e10d..3735316dc3478 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3166,17 +3166,18 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
auto Tokens =
- annotate("Ctor::Ctor(int x, int y) try\n"
- " : foo{[]() -> std::string { return {}; }(), x}, bar{y} {\n"
+ annotate("Foo::Foo(int x, int y) try\n"
+ " : foo{[] -> std::string { return {}; }(), x}, bar{y} {\n"
"} catch (...) {\n"
"}");
- ASSERT_EQ(Tokens.size(), 47u);
+ ASSERT_EQ(Tokens.size(), 45u);
+ EXPECT_TOKEN(Tokens[2], tok::identifier, TT_CtorDtorDeclName);
EXPECT_TOKEN(Tokens[11], tok::colon, TT_CtorInitializerColon);
EXPECT_TOKEN(Tokens[14], tok::l_square, TT_LambdaLSquare);
- EXPECT_TOKEN(Tokens[18], tok::arrow, TT_TrailingReturnArrow);
- EXPECT_TOKEN(Tokens[22], tok::l_brace, TT_LambdaLBrace);
- EXPECT_TOKEN(Tokens[33], tok::comma, TT_CtorInitializerComma);
- EXPECT_TOKEN(Tokens[38], tok::l_brace, TT_FunctionLBrace);
+ EXPECT_TOKEN(Tokens[16], tok::arrow, TT_TrailingReturnArrow);
+ EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_LambdaLBrace);
+ EXPECT_TOKEN(Tokens[31], tok::comma, TT_CtorInitializerComma);
+ EXPECT_TOKEN(Tokens[36], tok::l_brace, TT_FunctionLBrace);
}
} // namespace
More information about the cfe-commits
mailing list