[clang] db41c0b - [clang-format] PR35514 brace-init member initializers in function-try-blocks are not formatted correctly

via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 17 01:40:07 PST 2020


Author: mydeveloperday
Date: 2020-12-17T09:39:37Z
New Revision: db41c0b357d55ccd6206ff262dc50ed38f0d5474

URL: https://github.com/llvm/llvm-project/commit/db41c0b357d55ccd6206ff262dc50ed38f0d5474
DIFF: https://github.com/llvm/llvm-project/commit/db41c0b357d55ccd6206ff262dc50ed38f0d5474.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index ddb3a247429a..4c2ee421d092 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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.

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a3dbec9a669f..d2aed304f213 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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);


        


More information about the cfe-commits mailing list