Index: docs/ClangFormatStyleOptions.rst =================================================================== --- docs/ClangFormatStyleOptions.rst +++ docs/ClangFormatStyleOptions.rst @@ -481,6 +481,10 @@ **SpaceAfterCStyleCast** (``bool``) If ``true``, a space may be inserted after C style casts. +**SpaceAfterTemplateKeyword** (``bool``) + If ``true``, a space may be inserted between the 'template' keyword + and the following '<'. + **SpaceBeforeAssignmentOperators** (``bool``) If ``false``, spaces will be removed before assignment operators. Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -367,6 +367,10 @@ /// \brief If \c true, a space may be inserted after C style casts. bool SpaceAfterCStyleCast; + /// \brief If \c true, a space may be inserted between the 'template' keyword + /// and the following '<'. + bool SpaceAfterTemplateKeyword; + /// \brief If \c false, spaces will be removed before assignment operators. bool SpaceBeforeAssignmentOperators; @@ -512,6 +516,7 @@ PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine && PointerAlignment == R.PointerAlignment && SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && + SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && SpaceBeforeParens == R.SpaceBeforeParens && SpaceInEmptyParentheses == R.SpaceInEmptyParentheses && Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -268,6 +268,7 @@ Style.PenaltyReturnTypeOnItsOwnLine); IO.mapOptional("PointerAlignment", Style.PointerAlignment); IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); + IO.mapOptional("SpaceAfterTemplateKeyword", Style.SpaceAfterTemplateKeyword); IO.mapOptional("SpaceBeforeAssignmentOperators", Style.SpaceBeforeAssignmentOperators); IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); @@ -398,6 +399,7 @@ LLVMStyle.SpacesInContainerLiterals = true; LLVMStyle.SpacesInCStyleCastParentheses = false; LLVMStyle.SpaceAfterCStyleCast = false; + LLVMStyle.SpaceAfterTemplateKeyword = true; LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; LLVMStyle.SpaceBeforeAssignmentOperators = true; LLVMStyle.SpacesInAngles = false; Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -1827,7 +1827,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.SpaceAfterTemplateKeyword) || (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) return true; if (Left.isOneOf(tok::exclaim, tok::tilde)) Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -5728,6 +5728,17 @@ verifyFormat("template void Foo(Ts*... ts) {}", PointersLeft); } +TEST_F(FormatTest, SpaceAfterTemplate) { + FormatStyle Style = getLLVMStyle(); + Style.SpaceAfterTemplateKeyword = false; + verifyFormat("template<> class Foo {}", Style); + verifyFormat("template<> void Foo() {}", Style); + verifyFormat("template class Foo {}", Style); + verifyFormat("template void Foo() {}", Style); + verifyFormat("template class Foo {}", Style); + verifyFormat("template void Foo() {}", Style); +} + TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { EXPECT_EQ("int *a;\n" "int *a;\n"