Skip to content

[clang-format] Add BreakBeforeTemplateCloser option #118046

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 34 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1caf823
[clang-format] Add BreakBeforeTemplateClose option
leijurv Nov 30, 2024
212e444
add test case
leijurv Dec 7, 2024
3927d41
more tests
leijurv Dec 9, 2024
2c3a64c
more tests
leijurv Dec 22, 2024
abedc01
Merge branch 'main' into BreakBeforeTemplateClose
leijurv Dec 30, 2024
29742cd
Merge branch 'main' into BreakBeforeTemplateClose
leijurv Jan 2, 2025
f96441d
Merge branch 'main' into BreakBeforeTemplateClose
leijurv Jan 6, 2025
19bc40e
address comments
leijurv Jan 11, 2025
f75268a
address comments
leijurv Jan 12, 2025
1304288
address comments
leijurv Jan 12, 2025
9277a68
switch from bool to enum
leijurv Jan 12, 2025
0952882
switch from multiline to blockindent
leijurv Jan 13, 2025
ed0289f
one last test
leijurv Jan 13, 2025
53766dd
rerun dump format style
leijurv Jan 13, 2025
605ade3
Merge branch 'main' into BreakBeforeTemplateClose
leijurv Jan 14, 2025
deb01fc
Merge branch 'main' into BreakBeforeTemplateClose
leijurv Jan 16, 2025
b1a5445
Merge branch 'main' into BreakBeforeTemplateClose
leijurv Jan 20, 2025
fa15aca
address comments
leijurv Jan 20, 2025
c0090c8
address comments
leijurv Jan 28, 2025
2973647
Merge branch 'main' into BreakBeforeTemplateClose
leijurv Jan 28, 2025
9c684c8
Merge branch 'main' into BreakBeforeTemplateClose
leijurv Jan 28, 2025
dc65a0d
Merge branch 'main' into BreakBeforeTemplateClose
leijurv Jan 31, 2025
9381a00
bump version to 21
leijurv Jan 31, 2025
a437f23
also need to bump version here
leijurv Jan 31, 2025
a768adb
switch back to bool
leijurv Feb 2, 2025
e1eaba9
address comments
leijurv Feb 4, 2025
7d456ca
empty commit to kick github
leijurv Feb 5, 2025
b6219e2
address comment
leijurv Feb 5, 2025
2cc5a69
address comments
leijurv Feb 6, 2025
4faf28a
address comments
leijurv Feb 6, 2025
9cf6bcc
Update clang/include/clang/Format/Format.h
leijurv Feb 6, 2025
b5a5e96
regenerate docs
leijurv Feb 6, 2025
51f3b9a
NFC: minor cleanup
owenca Feb 6, 2025
00bad0e
Remove trailing whitespace in ContinuationIndenter.cpp
owenca Feb 6, 2025
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
29 changes: 29 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3421,6 +3421,35 @@ the configuration (without a prefix: ``Auto``).



.. _BreakBeforeTemplateCloser:

**BreakBeforeTemplateCloser** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakBeforeTemplateCloser>`
If ``true``, break before a template closing bracket (``>``) when there is
a line break after the matching opening bracket (``<``).

.. code-block:: c++

true:
template <typename Foo, typename Bar>

template <typename Foo,
typename Bar>

template <
typename Foo,
typename Bar
>

false:
template <typename Foo, typename Bar>

template <typename Foo,
typename Bar>

template <
typename Foo,
typename Bar>

.. _BreakBeforeTernaryOperators:

**BreakBeforeTernaryOperators** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <BreakBeforeTernaryOperators>`
Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ AST Matchers
clang-format
------------

- Adds ``BreakBeforeTemplateCloser`` option.

libclang
--------

Expand Down
28 changes: 28 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,33 @@ struct FormatStyle {
/// \version 16
BreakBeforeInlineASMColonStyle BreakBeforeInlineASMColon;

/// If ``true``, break before a template closing bracket (``>``) when there is
/// a line break after the matching opening bracket (``<``).
/// \code
/// true:
/// template <typename Foo, typename Bar>
///
/// template <typename Foo,
/// typename Bar>
///
/// template <
/// typename Foo,
/// typename Bar
/// >
///
/// false:
/// template <typename Foo, typename Bar>
///
/// template <typename Foo,
/// typename Bar>
///
/// template <
/// typename Foo,
/// typename Bar>
/// \endcode
/// \version 21
bool BreakBeforeTemplateCloser;

/// If ``true``, ternary operators will be placed after line breaks.
/// \code
/// true:
Expand Down Expand Up @@ -5251,6 +5278,7 @@ struct FormatStyle {
BreakBeforeBraces == R.BreakBeforeBraces &&
BreakBeforeConceptDeclarations == R.BreakBeforeConceptDeclarations &&
BreakBeforeInlineASMColon == R.BreakBeforeInlineASMColon &&
BreakBeforeTemplateCloser == R.BreakBeforeTemplateCloser &&
BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators &&
BreakBinaryOperations == R.BreakBinaryOperations &&
BreakConstructorInitializers == R.BreakConstructorInitializers &&
Expand Down
24 changes: 18 additions & 6 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,22 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
}
}

// Don't allow breaking before a closing brace of a block-indented braced list
// initializer if there isn't already a break.
if (Current.is(tok::r_brace) && Current.MatchingParen &&
Current.isBlockIndentedInitRBrace(Style)) {
return CurrentState.BreakBeforeClosingBrace;
}

// Allow breaking before the right parens with block indentation if there was
// a break after the left parens, which is tracked by BreakBeforeClosingParen.
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
Current.is(tok::r_paren)) {
return CurrentState.BreakBeforeClosingParen;
}

// Don't allow breaking before a closing brace of a block-indented braced list
// initializer if there isn't already a break.
if (Current.is(tok::r_brace) && Current.MatchingParen &&
Current.isBlockIndentedInitRBrace(Style)) {
return CurrentState.BreakBeforeClosingBrace;
}
if (Style.BreakBeforeTemplateCloser && Current.is(TT_TemplateCloser))
return CurrentState.BreakBeforeClosingAngle;

// If binary operators are moved to the next line (including commas for some
// styles of constructor initializers), that's always ok.
Expand Down Expand Up @@ -414,6 +417,8 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
}
if (CurrentState.BreakBeforeClosingParen && Current.is(tok::r_paren))
return true;
if (CurrentState.BreakBeforeClosingAngle && Current.is(TT_TemplateCloser))
return true;
if (Style.Language == FormatStyle::LK_ObjC &&
Style.ObjCBreakBeforeNestedBlockParam &&
Current.ObjCSelectorNameParts > 1 &&
Expand Down Expand Up @@ -1243,6 +1248,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
}

if (PreviousNonComment && PreviousNonComment->is(TT_TemplateOpener))
CurrentState.BreakBeforeClosingAngle = Style.BreakBeforeTemplateCloser;

if (CurrentState.AvoidBinPacking) {
// If we are breaking after '(', '{', '<', or this is the break after a ':'
// to start a member initializer list in a constructor, this should not
Expand Down Expand Up @@ -1379,6 +1387,10 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
if (Style.BreakBeforeTemplateCloser && Current.is(TT_TemplateCloser) &&
State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
return State.Stack[State.Stack.size() - 2].LastSpace;
// Field labels in a nested type should be aligned to the brace. For example
Expand Down
22 changes: 14 additions & 8 deletions clang/lib/Format/ContinuationIndenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,15 @@ struct ParenState {
: Tok(Tok), Indent(Indent), LastSpace(LastSpace),
NestedBlockIndent(Indent), IsAligned(false),
BreakBeforeClosingBrace(false), BreakBeforeClosingParen(false),
AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
NoLineBreak(NoLineBreak), NoLineBreakInOperand(false),
LastOperatorWrapped(true), ContainsLineBreak(false),
ContainsUnwrappedBuilder(false), AlignColons(true),
ObjCSelectorNameFound(false), HasMultipleNestedBlocks(false),
NestedBlockInlined(false), IsInsideObjCArrayLiteral(false),
IsCSharpGenericTypeConstraint(false), IsChainedConditional(false),
IsWrappedConditional(false), UnindentOperator(false) {}
BreakBeforeClosingAngle(false), AvoidBinPacking(AvoidBinPacking),
BreakBeforeParameter(false), NoLineBreak(NoLineBreak),
NoLineBreakInOperand(false), LastOperatorWrapped(true),
ContainsLineBreak(false), ContainsUnwrappedBuilder(false),
AlignColons(true), ObjCSelectorNameFound(false),
HasMultipleNestedBlocks(false), NestedBlockInlined(false),
IsInsideObjCArrayLiteral(false), IsCSharpGenericTypeConstraint(false),
IsChainedConditional(false), IsWrappedConditional(false),
UnindentOperator(false) {}

/// \brief The token opening this parenthesis level, or nullptr if this level
/// is opened by fake parenthesis.
Expand Down Expand Up @@ -280,6 +281,9 @@ struct ParenState {
/// was a newline after the beginning left paren.
bool BreakBeforeClosingParen : 1;

/// Whether a newline needs to be inserted before a closing angle `>`.
bool BreakBeforeClosingAngle : 1;

/// Avoid bin packing, i.e. multiple parameters/elements on multiple
/// lines, in this context.
bool AvoidBinPacking : 1;
Expand Down Expand Up @@ -367,6 +371,8 @@ struct ParenState {
return BreakBeforeClosingBrace;
if (BreakBeforeClosingParen != Other.BreakBeforeClosingParen)
return BreakBeforeClosingParen;
if (BreakBeforeClosingAngle != Other.BreakBeforeClosingAngle)
return BreakBeforeClosingAngle;
if (QuestionColumn != Other.QuestionColumn)
return QuestionColumn < Other.QuestionColumn;
if (AvoidBinPacking != Other.AvoidBinPacking)
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
IO.mapOptional("BreakBeforeInlineASMColon",
Style.BreakBeforeInlineASMColon);
IO.mapOptional("BreakBeforeTemplateCloser",
Style.BreakBeforeTemplateCloser);
IO.mapOptional("BreakBeforeTernaryOperators",
Style.BreakBeforeTernaryOperators);
IO.mapOptional("BreakBinaryOperations", Style.BreakBinaryOperations);
Expand Down Expand Up @@ -1535,6 +1537,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
LLVMStyle.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Always;
LLVMStyle.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
LLVMStyle.BreakBeforeTemplateCloser = false;
LLVMStyle.BreakBeforeTernaryOperators = true;
LLVMStyle.BreakBinaryOperations = FormatStyle::BBO_Never;
LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6180,6 +6180,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
return false;
}

if (Right.is(TT_TemplateCloser))
return Style.BreakBeforeTemplateCloser;

if (Left.is(tok::at))
return false;
if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
Expand Down Expand Up @@ -6328,8 +6331,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
if (Right.is(TT_ImplicitStringLiteral))
return false;

if (Right.is(TT_TemplateCloser))
return false;
if (Right.is(tok::r_square) && Right.MatchingParen &&
Right.MatchingParen->is(TT_LambdaLSquare)) {
return false;
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(BinPackArguments);
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
CHECK_PARSE_BOOL(BreakBeforeTemplateCloser);
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
CHECK_PARSE_BOOL(BreakStringLiterals);
CHECK_PARSE_BOOL(CompactNamespaces);
Expand Down
Loading
Loading