diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index f86be2c1246fb..0a8cc18c5b4cb 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -1664,6 +1664,18 @@ the configuration (without a prefix: ``Auto``). Possible values: + * ``BTDS_Leave`` (in configuration: ``Leave``) + Do not change the line breaking before the declaration. + + .. code-block:: c++ + + template + T foo() { + } + template T foo(int aaaaaaaaaaaaaaaaaaaaa, + int bbbbbbbbbbbbbbbbbbbbb) { + } + * ``BTDS_No`` (in configuration: ``No``) Do not force break before declaration. ``PenaltyBreakTemplateDeclaration`` is taken into account. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 415321310c24f..cb14d98825400 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1031,6 +1031,16 @@ struct FormatStyle { /// Different ways to break after the template declaration. enum BreakTemplateDeclarationsStyle : int8_t { + /// Do not change the line breaking before the declaration. + /// \code + /// template + /// T foo() { + /// } + /// template T foo(int aaaaaaaaaaaaaaaaaaaaa, + /// int bbbbbbbbbbbbbbbbbbbbb) { + /// } + /// \endcode + BTDS_Leave, /// Do not force break before declaration. /// ``PenaltyBreakTemplateDeclaration`` is taken into account. /// \code diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 671ae540c75b1..7fd04b23abdca 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -569,7 +569,10 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { return true; } } - return Style.AlwaysBreakTemplateDeclarations != FormatStyle::BTDS_No; + return Style.AlwaysBreakTemplateDeclarations != FormatStyle::BTDS_No && + (Style.AlwaysBreakTemplateDeclarations != + FormatStyle::BTDS_Leave || + Current.NewlinesBefore > 0); } if (Previous.is(TT_FunctionAnnotationRParen) && State.Line->Type != LT_PreprocessorDirective) { diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 9c780cd7a5f4e..c5714af155f0b 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -296,6 +296,7 @@ template <> struct ScalarEnumerationTraits { static void enumeration(IO &IO, FormatStyle::BreakTemplateDeclarationsStyle &Value) { + IO.enumCase(Value, "Leave", FormatStyle::BTDS_Leave); IO.enumCase(Value, "No", FormatStyle::BTDS_No); IO.enumCase(Value, "MultiLine", FormatStyle::BTDS_MultiLine); IO.enumCase(Value, "Yes", FormatStyle::BTDS_Yes); diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 7a62f5fd3dfbb..cec56fad53156 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5184,7 +5184,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, // concept ... if (Right.is(tok::kw_concept)) return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always; - return Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes; + return Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes || + (Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Leave && + Right.NewlinesBefore > 0); } if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) { switch (Style.RequiresClausePosition) { @@ -5617,7 +5619,11 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never; if (Right.is(TT_RequiresClause)) return true; - if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen)) + if (Left.ClosesTemplateDeclaration) { + return Style.AlwaysBreakTemplateDeclarations != FormatStyle::BTDS_Leave || + Right.NewlinesBefore > 0; + } + if (Left.is(TT_FunctionAnnotationRParen)) return true; if (Left.ClosesRequiresClause) return true; diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 3f1fc89351694..7493b0a4450f9 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -695,6 +695,8 @@ TEST(ConfigParseTest, ParsesConfiguration) { FormatStyle::RTBS_TopLevelDefinitions); Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; + CHECK_PARSE("AlwaysBreakTemplateDeclarations: Leave", + AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Leave); CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 87a02a4dfbf2b..b1a2247bb85d6 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -10697,6 +10697,74 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) { verifyFormat("template void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " "bbbbbbbbbbbbbbbbbbbb) {}", NeverBreak); + + auto Style = getLLVMStyle(); + Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Leave; + + verifyNoChange("template \n" + "class C {};", + Style); + verifyFormat("template class C {};", Style); + + verifyNoChange("template \n" + "void f();", + Style); + verifyFormat("template void f();", Style); + + verifyNoChange("template \n" + "void f() {}", + Style); + verifyFormat("template void f() {}", Style); + + verifyNoChange("template \n" + "// T can be A, B or C.\n" + "struct C {};", + Style); + verifyFormat("template // T can be A, B or C.\n" + "struct C {};", + Style); + + verifyNoChange("template \n" + "C(T) noexcept;", + Style); + verifyFormat("template C(T) noexcept;", Style); + + verifyNoChange("template \n" + "class A {\n" + "public:\n" + " E *f();\n" + "};", + Style); + verifyFormat("template class A {\n" + "public:\n" + " E *f();\n" + "};", + Style); + + verifyNoChange("template \n" + "constexpr int simple(int) {\n" + " char c;\n" + " return 1;\n" + "}", + Style); + verifyFormat("template constexpr int simple(int) {\n" + " char c;\n" + " return 1;\n" + "}", + Style); + + Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; + verifyNoChange("template \n" + "requires(x > 1)\n" + "constexpr int with_req(int) {\n" + " return 1;\n" + "}", + Style); + verifyFormat("template requires(x > 1)\n" + "constexpr int with_req(int) {\n" + " return 1;\n" + "}", + Style); } TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {