aboutsummaryrefslogtreecommitdiff
path: root/clang
diff options
context:
space:
mode:
authorOwen Pan <owenpiano@gmail.com>2024-06-18 18:56:33 -0700
committerGitHub <noreply@github.com>2024-06-18 18:56:33 -0700
commit61571e9046fa6f12661e443123635053186794a1 (patch)
tree5ffedc687d0c921bf2a307d2ed74e44e61a465cd /clang
parenta5128542495d04e20db0828bceaae69c39e72c6d (diff)
downloadllvm-61571e9046fa6f12661e443123635053186794a1.zip
llvm-61571e9046fa6f12661e443123635053186794a1.tar.gz
llvm-61571e9046fa6f12661e443123635053186794a1.tar.bz2
[clang-format] Handle function try block with ctor-initializer (#95878)
Fixes #58987. Fixes #95679.
Diffstat (limited to 'clang')
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp17
-rw-r--r--clang/unittests/Format/TokenAnnotatorTest.cpp16
2 files changed, 27 insertions, 6 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index df5bb75..ce877ac 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 3e9638d..3735316 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3164,6 +3164,22 @@ TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
}
+TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
+ auto Tokens =
+ annotate("Foo::Foo(int x, int y) try\n"
+ " : foo{[] -> std::string { return {}; }(), x}, bar{y} {\n"
+ "} catch (...) {\n"
+ "}");
+ 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[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
} // namespace format
} // namespace clang