From e4ada024b0442639907f2005dc4b735b05d225bc Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 13 Dec 2016 10:05:03 +0000 Subject: [PATCH] clang-format: Improve braced-list detection. Before: vector v { 12 } GUARDED_BY(mutex); After: vector v{12} GUARDED_BY(mutex); llvm-svn: 289525 --- clang/lib/Format/UnwrappedLineParser.cpp | 4 ++-- clang/unittests/Format/FormatTest.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 52278d1..84e06d0 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -360,8 +360,6 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { // BlockKind later if we parse a braced list (where all blocks // inside are by default braced lists), or when we explicitly detect // blocks (for example while parsing lambdas). - // - // We exclude + and - as they can be ObjC visibility modifiers. ProbablyBracedList = (Style.Language == FormatStyle::LK_JavaScript && NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, @@ -369,6 +367,8 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { NextTok->isOneOf(tok::comma, tok::period, tok::colon, tok::r_paren, tok::r_square, tok::l_brace, tok::l_square, tok::l_paren, tok::ellipsis) || + (NextTok->is(tok::identifier) && + !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) || (NextTok->is(tok::semi) && (!ExpectClassBody || LBraceStack.size() != 1)) || (NextTok->isBinaryOperator() && !NextIsObjCMethod); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index fe2d470..5bed7a8 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -6504,6 +6504,19 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { "};"); verifyFormat("#define A {a, a},"); + // Cases where distinguising braced lists and blocks is hard. + verifyFormat("vector v{12} GUARDED_BY(mutex);"); + verifyFormat("void f() {\n" + " return; // comment\n" + "}\n" + "SomeType t;"); + verifyFormat("void f() {\n" + " if (a) {\n" + " f();\n" + " }\n" + "}\n" + "SomeType t;"); + // In combination with BinPackArguments = false. FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackArguments = false; -- 2.7.4