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

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 15 06:31:28 PST 2020


MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: krasimir, curdeius, JakeMerdichAMD, mnem.
MyDeveloperDay added projects: clang-format, clang.
MyDeveloperDay requested review of this revision.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93296

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2727,6 +2727,20 @@
                "    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");
 
   // Incomplete try-catch blocks.
   verifyIncompleteFormat("try {} catch (");
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2050,6 +2050,13 @@
       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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93296.311886.patch
Type: text/x-patch
Size: 1532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201215/b6208e3f/attachment.bin>


More information about the cfe-commits mailing list