Skip to content

[clang-format] Add Leave to AlwaysBreakTemplateDeclarations #80569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,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 <typename T>
T foo() {
}
template <typename T> T foo(int aaaaaaaaaaaaaaaaaaaaa,
int bbbbbbbbbbbbbbbbbbbbb) {
}

* ``BTDS_No`` (in configuration: ``No``)
Do not force break before declaration.
``PenaltyBreakTemplateDeclaration`` is taken into account.
Expand Down
10 changes: 10 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,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 <typename T>
/// T foo() {
/// }
/// template <typename T> T foo(int aaaaaaaaaaaaaaaaaaaaa,
/// int bbbbbbbbbbbbbbbbbbbbb) {
/// }
/// \endcode
BTDS_Leave,
/// Do not force break before declaration.
/// ``PenaltyBreakTemplateDeclaration`` is taken into account.
/// \code
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,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) {
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ template <>
struct ScalarEnumerationTraits<FormatStyle::BreakTemplateDeclarationsStyle> {
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);
Expand Down
10 changes: 8 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5182,7 +5182,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) {
Expand Down Expand Up @@ -5615,7 +5617,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;
Expand Down
2 changes: 2 additions & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,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",
Expand Down
68 changes: 68 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10657,6 +10657,74 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
"bbbbbbbbbbbbbbbbbbbb) {}",
NeverBreak);

auto Style = getLLVMStyle();
Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Leave;

verifyNoChange("template <typename T>\n"
"class C {};",
Style);
verifyFormat("template <typename T> class C {};", Style);

verifyNoChange("template <typename T>\n"
"void f();",
Style);
verifyFormat("template <typename T> void f();", Style);

verifyNoChange("template <typename T>\n"
"void f() {}",
Style);
verifyFormat("template <typename T> void f() {}", Style);

verifyNoChange("template <typename T>\n"
"// T can be A, B or C.\n"
"struct C {};",
Style);
verifyFormat("template <typename T> // T can be A, B or C.\n"
"struct C {};",
Style);

verifyNoChange("template <typename T>\n"
"C(T) noexcept;",
Style);
verifyFormat("template <typename T> C(T) noexcept;", Style);

verifyNoChange("template <enum E>\n"
"class A {\n"
"public:\n"
" E *f();\n"
"};",
Style);
verifyFormat("template <enum E> class A {\n"
"public:\n"
" E *f();\n"
"};",
Style);

verifyNoChange("template <auto x>\n"
"constexpr int simple(int) {\n"
" char c;\n"
" return 1;\n"
"}",
Style);
verifyFormat("template <auto x> constexpr int simple(int) {\n"
" char c;\n"
" return 1;\n"
"}",
Style);

Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
verifyNoChange("template <auto x>\n"
"requires(x > 1)\n"
"constexpr int with_req(int) {\n"
" return 1;\n"
"}",
Style);
verifyFormat("template <auto x> requires(x > 1)\n"
"constexpr int with_req(int) {\n"
" return 1;\n"
"}",
Style);
}

TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
Expand Down