diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -492,6 +492,9 @@ // update information about whether an lbrace starts a // braced init list or a different block during the loop. SmallVector LBraceStack; + // Track the previous token type corresponding to our lbraces // to help + // detect brace types + SmallVector PrevTokenKindStack; assert(Tok->is(tok::l_brace)); do { // Get next non-comment token. @@ -522,6 +525,8 @@ Tok->setBlockKind(BK_Unknown); } LBraceStack.push_back(Tok); + PrevTokenKindStack.push_back(PrevTok ? PrevTok->Tok.getKind() + : tok::unknown); break; case tok::r_brace: if (LBraceStack.empty()) @@ -570,8 +575,13 @@ ProbablyBracedList = ProbablyBracedList || NextTok->isOneOf(tok::comma, tok::period, tok::colon, - tok::r_paren, tok::r_square, tok::l_brace, - tok::ellipsis); + tok::r_paren, tok::r_square, tok::ellipsis); + + // Distinguish between braced list in a constructor initializer list + // followed by constructor body, or just adjacent blocks + ProbablyBracedList = ProbablyBracedList || + NextTok->is(tok::l_brace) && + PrevTokenKindStack.back() == tok::identifier; ProbablyBracedList = ProbablyBracedList || @@ -602,6 +612,7 @@ } } LBraceStack.pop_back(); + PrevTokenKindStack.pop_back(); break; case tok::identifier: if (!Tok->is(TT_StatementMacro)) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -13731,6 +13731,26 @@ " struct Dummy {};\n" " f(v);\n" "}"); + verifyFormat("void foo() {\n" + " { // asdf\n" + " { int a; }\n" + " }\n" + " {\n" + " { int b; }\n" + " }\n" + "}"); + verifyFormat("namespace n {\n" + "void foo() {\n" + " {\n" + " {\n" + " statement();\n" + " if (false) {\n" + " }\n" + " }\n" + " }\n" + " {}\n" + "}\n" + "} // namespace n"); // Long lists should be formatted in columns even if they are nested. verifyFormat(