Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -529,6 +529,9 @@ /// \brief If ``true``, a space may be inserted after C style casts. bool SpaceAfterCStyleCast; + /// \brief If ``true``, a space may be inserted after the template keyword. + bool SpaceAfterTemplate; + /// \brief If ``false``, spaces will be removed before assignment operators. bool SpaceBeforeAssignmentOperators; @@ -564,6 +567,9 @@ /// in template argument lists. bool SpacesInAngles; + /// \brief If ``true``, spaces may be inserted inside brace blocks. + bool SpacesInBraces; + /// \brief If ``true``, spaces are inserted inside container literals (e.g. /// ObjC and Javascript array and dict literals). bool SpacesInContainerLiterals; @@ -688,18 +694,19 @@ PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine && PointerAlignment == R.PointerAlignment && SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && + SpaceAfterTemplate == R.SpaceAfterTemplate && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && SpaceBeforeParens == R.SpaceBeforeParens && SpaceInEmptyParentheses == R.SpaceInEmptyParentheses && SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments && SpacesInAngles == R.SpacesInAngles && + SpacesInBraces == R.SpacesInBraces && SpacesInContainerLiterals == R.SpacesInContainerLiterals && SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses && SpacesInParentheses == R.SpacesInParentheses && SpacesInSquareBrackets == R.SpacesInSquareBrackets && Standard == R.Standard && TabWidth == R.TabWidth && - UseTab == R.UseTab && - JavaScriptQuotes == R.JavaScriptQuotes; + UseTab == R.UseTab && JavaScriptQuotes == R.JavaScriptQuotes; } }; Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -328,6 +328,7 @@ IO.mapOptional("ReflowComments", Style.ReflowComments); IO.mapOptional("SortIncludes", Style.SortIncludes); IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); + IO.mapOptional("SpaceAfterTemplate", Style.SpaceAfterTemplate); IO.mapOptional("SpaceBeforeAssignmentOperators", Style.SpaceBeforeAssignmentOperators); IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); @@ -335,6 +336,7 @@ IO.mapOptional("SpacesBeforeTrailingComments", Style.SpacesBeforeTrailingComments); IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); + IO.mapOptional("SpacesInBraces", Style.SpacesInBraces); IO.mapOptional("SpacesInContainerLiterals", Style.SpacesInContainerLiterals); IO.mapOptional("SpacesInCStyleCastParentheses", @@ -541,9 +543,11 @@ LLVMStyle.SpacesInContainerLiterals = true; LLVMStyle.SpacesInCStyleCastParentheses = false; LLVMStyle.SpaceAfterCStyleCast = false; + LLVMStyle.SpaceAfterTemplate = true; LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; LLVMStyle.SpaceBeforeAssignmentOperators = true; LLVMStyle.SpacesInAngles = false; + LLVMStyle.SpacesInBraces = false; LLVMStyle.PenaltyBreakComment = 300; LLVMStyle.PenaltyBreakFirstLessLess = 120; Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -1938,7 +1938,7 @@ if (Right.isOneOf(tok::semi, tok::comma)) return false; if (Right.is(tok::less) && - (Left.is(tok::kw_template) || + (Left.is(tok::kw_template) && Style.SpaceAfterTemplate || (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) return true; if (Left.isOneOf(tok::exclaim, tok::tilde)) @@ -2001,11 +2001,12 @@ !Left.isOneOf(tok::numeric_constant, TT_DictLiteral)) return false; if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) - return !Left.Children.empty(); // No spaces in "{}". + return Style.SpacesInBraces ? true + : !Left.Children.empty(); // No spaces in "{}". 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.SpacesInBraces || !Style.Cpp11BracedListStyle; if (Left.is(TT_BlockComment)) return !Left.TokenText.endswith("=*/"); if (Right.is(tok::l_paren)) { Index: lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- lib/Format/UnwrappedLineFormatter.cpp +++ lib/Format/UnwrappedLineFormatter.cpp @@ -358,7 +358,7 @@ (Tok->getNextNonComment() == nullptr || Tok->getNextNonComment()->is(tok::semi))) { // We merge empty blocks even if the line exceeds the column limit. - Tok->SpacesRequiredBefore = 0; + Tok->SpacesRequiredBefore = Style.SpacesInBraces ? 1 : 0; Tok->CanBreakBefore = true; return 1; } else if (Limit != 0 && !Line.startsWith(tok::kw_namespace) && Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -11058,6 +11058,24 @@ verifyFormat("A>();", Spaces); } +TEST_F(FormatTest, SpaceAfterTemplate) { + FormatStyle Style = getLLVMStyle(); + + verifyFormat("template void foo();", Style); + + Style.SpaceAfterTemplate = false; + verifyFormat("template void foo();", Style); +} + +TEST_F(FormatTest, SpacesInBraces) { + FormatStyle Style = getLLVMStyle(); + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; + Style.SpacesInBraces = true; + + verifyFormat("int x[] = { 1, 2, 3, 4 };", Style); + verifyFormat("void foo() { }", Style); +} + TEST_F(FormatTest, TripleAngleBrackets) { verifyFormat("f<<<1, 1>>>();"); verifyFormat("f<<<1, 1, 1, s>>>();");