Author: paulhoad Date: Fri Oct 4 07:25:20 2019 New Revision: 373751 URL: http://llvm.org/viewvc/llvm-project?rev=373751&view=rev Log: [clang-format] C++11 braced lists should respect the SpacesInParentheses setting
Summary: According to the clang-format documentation, "Fundamentally, C++11 braced lists are formatted exactly like function calls would be formatted in their place. If the braced list follows a name (e.g. a type or variable name), clang-format formats as if the `{}` were the parentheses of a function call with that name." This patch furthers the treatment of C++11 braced list braces as parentheses by respecting the `SpacesInParentheses` setting. Reviewers: MyDeveloperDay, reuk, owenpan Reviewed By: MyDeveloperDay Subscribers: cfe-commits Tags: #clang-format, #clang Patch By: mitchell-stellar Differential Revision: https://reviews.llvm.org/D68415 Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp cfe/trunk/unittests/Format/FormatTest.cpp Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=373751&r1=373750&r2=373751&view=diff ============================================================================== --- cfe/trunk/lib/Format/TokenAnnotator.cpp (original) +++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Oct 4 07:25:20 2019 @@ -2196,7 +2196,8 @@ void TokenAnnotator::calculateFormatting if (Current->is(TT_LineComment)) { if (Current->Previous->BlockKind == BK_BracedInit && Current->Previous->opensScope()) - Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1; + Current->SpacesRequiredBefore = + (Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1; else Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; @@ -2520,7 +2521,9 @@ bool TokenAnnotator::spaceRequiredBetwee return Left.is(tok::hash); if (Left.isOneOf(tok::hashhash, tok::hash)) return Right.is(tok::hash); - if (Left.is(tok::l_paren) && Right.is(tok::r_paren)) + if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) || + (Left.is(tok::l_brace) && Left.BlockKind != BK_Block && + Right.is(tok::r_brace) && Right.BlockKind != BK_Block)) return Style.SpaceInEmptyParentheses; if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) return (Right.is(TT_CastRParen) || @@ -2636,7 +2639,7 @@ bool TokenAnnotator::spaceRequiredBetwee if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) || (Right.is(tok::r_brace) && Right.MatchingParen && Right.MatchingParen->BlockKind != BK_Block)) - return !Style.Cpp11BracedListStyle; + return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true; if (Left.is(TT_BlockComment)) // No whitespace in x(/*foo=*/1), except for JavaScript. return Style.Language == FormatStyle::LK_JavaScript || Modified: cfe/trunk/unittests/Format/FormatTest.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=373751&r1=373750&r2=373751&view=diff ============================================================================== --- cfe/trunk/unittests/Format/FormatTest.cpp (original) +++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Oct 4 07:25:20 2019 @@ -8214,6 +8214,34 @@ TEST_F(FormatTest, LayoutCxx11BraceIniti SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); + + FormatStyle SpaceBetweenBraces = getLLVMStyle(); + SpaceBetweenBraces.SpacesInAngles = true; + SpaceBetweenBraces.SpacesInParentheses = true; + SpaceBetweenBraces.SpacesInSquareBrackets = true; + verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); + verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); + verifyFormat("vector< int > x{ // comment 1\n" + " 1, 2, 3, 4 };", + SpaceBetweenBraces); + SpaceBetweenBraces.ColumnLimit = 20; + EXPECT_EQ("vector< int > x{\n" + " 1, 2, 3, 4 };", + format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); + SpaceBetweenBraces.ColumnLimit = 24; + EXPECT_EQ("vector< int > x{ 1, 2,\n" + " 3, 4 };", + format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); + EXPECT_EQ("vector< int > x{\n" + " 1,\n" + " 2,\n" + " 3,\n" + " 4,\n" + "};", + format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); + verifyFormat("vector< int > x{};", SpaceBetweenBraces); + SpaceBetweenBraces.SpaceInEmptyParentheses = true; + verifyFormat("vector< int > x{ };", SpaceBetweenBraces); } TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits