Skip to content

Commit db41c0b

Browse files
[clang-format] PR35514 brace-init member initializers in function-try-blocks are not formatted correctly
https://bugs.llvm.org/show_bug.cgi?id=35514 Initializer lists with a try-block are incorrectly formatted. e.g. ``` Foo(int abc, int def) try : _abc(abc), _def{def}, _ghi{1} { callA(); callB(); } catch (std::exception&) { } ``` is formatted as: ``` Foo(int abc, int def) try : _abc(abc), _def { def } , _ghi{1} { callA(); callB(); } catch (std::exception&) { } ``` This revision adds support in the parseTryCatch for braced initializers in the initializer list Reviewed By: curdeius, HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D93296
1 parent 7f8779e commit db41c0b

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,13 @@ void UnwrappedLineParser::parseTryCatch() {
20502050
nextToken();
20512051
if (FormatTok->is(tok::l_paren))
20522052
parseParens();
2053+
if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) &&
2054+
FormatTok->is(tok::l_brace)) {
2055+
do {
2056+
nextToken();
2057+
} while (!FormatTok->is(tok::r_brace));
2058+
nextToken();
2059+
}
20532060

20542061
// In case identifiers were removed by clang-tidy, what might follow is
20552062
// multiple commas in sequence - after the first identifier.

clang/unittests/Format/FormatTest.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,29 @@ TEST_F(FormatTest, FormatTryCatch) {
27272727
" throw;\n"
27282728
" }\n"
27292729
"};\n");
2730+
verifyFormat("class A {\n"
2731+
" int a;\n"
2732+
" A() try : a(0), b{1} {\n"
2733+
" } catch (...) {\n"
2734+
" throw;\n"
2735+
" }\n"
2736+
"};\n");
2737+
verifyFormat("class A {\n"
2738+
" int a;\n"
2739+
" A() try : a(0), b{1}, c{2} {\n"
2740+
" } catch (...) {\n"
2741+
" throw;\n"
2742+
" }\n"
2743+
"};\n");
2744+
verifyFormat("class A {\n"
2745+
" int a;\n"
2746+
" A() try : a(0), b{1}, c{2} {\n"
2747+
" { // New scope.\n"
2748+
" }\n"
2749+
" } catch (...) {\n"
2750+
" throw;\n"
2751+
" }\n"
2752+
"};\n");
27302753

27312754
// Incomplete try-catch blocks.
27322755
verifyIncompleteFormat("try {} catch (");
@@ -7756,8 +7779,8 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) {
77567779
verifyFormat("co_yield -1;");
77577780
verifyFormat("co_return -1;");
77587781

7759-
// Check that * is not treated as a binary operator when we set PointerAlignment
7760-
// as PAS_Left after a keyword and not a declaration.
7782+
// Check that * is not treated as a binary operator when we set
7783+
// PointerAlignment as PAS_Left after a keyword and not a declaration.
77617784
FormatStyle PASLeftStyle = getLLVMStyle();
77627785
PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
77637786
verifyFormat("co_return *a;", PASLeftStyle);

0 commit comments

Comments
 (0)