[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
This commit is contained in:
mydeveloperday 2020-12-17 09:39:37 +00:00
parent 7f8779e4e6
commit db41c0b357
2 changed files with 32 additions and 2 deletions

View File

@ -2050,6 +2050,13 @@ void UnwrappedLineParser::parseTryCatch() {
nextToken();
if (FormatTok->is(tok::l_paren))
parseParens();
if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) &&
FormatTok->is(tok::l_brace)) {
do {
nextToken();
} while (!FormatTok->is(tok::r_brace));
nextToken();
}
// In case identifiers were removed by clang-tidy, what might follow is
// multiple commas in sequence - after the first identifier.

View File

@ -2727,6 +2727,29 @@ TEST_F(FormatTest, FormatTryCatch) {
" throw;\n"
" }\n"
"};\n");
verifyFormat("class A {\n"
" int a;\n"
" A() try : a(0), b{1} {\n"
" } catch (...) {\n"
" throw;\n"
" }\n"
"};\n");
verifyFormat("class A {\n"
" int a;\n"
" A() try : a(0), b{1}, c{2} {\n"
" } catch (...) {\n"
" throw;\n"
" }\n"
"};\n");
verifyFormat("class A {\n"
" int a;\n"
" A() try : a(0), b{1}, c{2} {\n"
" { // New scope.\n"
" }\n"
" } catch (...) {\n"
" throw;\n"
" }\n"
"};\n");
// Incomplete try-catch blocks.
verifyIncompleteFormat("try {} catch (");
@ -7756,8 +7779,8 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) {
verifyFormat("co_yield -1;");
verifyFormat("co_return -1;");
// Check that * is not treated as a binary operator when we set PointerAlignment
// as PAS_Left after a keyword and not a declaration.
// Check that * is not treated as a binary operator when we set
// PointerAlignment as PAS_Left after a keyword and not a declaration.
FormatStyle PASLeftStyle = getLLVMStyle();
PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
verifyFormat("co_return *a;", PASLeftStyle);