Skip to content

Commit 0952882

Browse files
committed
switch from multiline to blockindent
1 parent 9277a68 commit 0952882

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3436,17 +3436,23 @@ the configuration (without a prefix: ``Auto``).
34363436

34373437
template <typename Foo, typename Bar>
34383438

3439+
template <typename Foo,
3440+
typename Bar>
3441+
34393442
template <
34403443
typename Foo,
34413444
typename Bar>
34423445

3443-
* ``BBTCS_Multiline`` (in configuration: ``Multiline``)
3444-
Break before a template closer if the template spans more than one line.
3446+
* ``BBTCS_BlockIndent`` (in configuration: ``BlockIndent``)
3447+
Break before a template closer if the template has broken into block indent style.
34453448

34463449
.. code-block:: c++
34473450

34483451
template <typename Foo, typename Bar>
34493452

3453+
template <typename Foo,
3454+
typename Bar>
3455+
34503456
template <
34513457
typename Foo,
34523458
typename Bar

clang/include/clang/Format/Format.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,21 +2258,28 @@ struct FormatStyle {
22582258
/// \code
22592259
/// template <typename Foo, typename Bar>
22602260
///
2261+
/// template <typename Foo,
2262+
/// typename Bar>
2263+
///
22612264
/// template <
22622265
/// typename Foo,
22632266
/// typename Bar>
22642267
/// \endcode
22652268
BBTCS_Never,
2266-
/// Break before a template closer if the template spans more than one line.
2269+
/// Break before a template closer if the template has broken into block
2270+
/// indent style.
22672271
/// \code
22682272
/// template <typename Foo, typename Bar>
22692273
///
2274+
/// template <typename Foo,
2275+
/// typename Bar>
2276+
///
22702277
/// template <
22712278
/// typename Foo,
22722279
/// typename Bar
22732280
/// >
22742281
/// \endcode
2275-
BBTCS_Multiline,
2282+
BBTCS_BlockIndent,
22762283
};
22772284

22782285
/// The style of when a line break will be placed before the ``>`` that closes

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
407407
if (CurrentState.BreakBeforeClosingParen && Current.is(tok::r_paren))
408408
return true;
409409
if (CurrentState.BreakBeforeClosingAngle && Current.is(TT_TemplateCloser) &&
410-
Style.BreakBeforeTemplateCloser == FormatStyle::BBTCS_Multiline) {
410+
Style.BreakBeforeTemplateCloser == FormatStyle::BBTCS_BlockIndent) {
411411
return true;
412412
}
413413
if (Style.Language == FormatStyle::LK_ObjC &&

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ struct ScalarEnumerationTraits<FormatStyle::BreakBeforeTemplateCloserStyle> {
261261
static void enumeration(IO &IO,
262262
FormatStyle::BreakBeforeTemplateCloserStyle &Value) {
263263
IO.enumCase(Value, "Never", FormatStyle::BBTCS_Never);
264-
IO.enumCase(Value, "Multiline", FormatStyle::BBTCS_Multiline);
264+
IO.enumCase(Value, "BlockIndent", FormatStyle::BBTCS_BlockIndent);
265265
}
266266
};
267267

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
424424
FormatStyle::BOS_All);
425425

426426
Style.BreakBeforeTemplateCloser = FormatStyle::BBTCS_Never;
427-
CHECK_PARSE("BreakBeforeTemplateCloser: Multiline", BreakBeforeTemplateCloser,
428-
FormatStyle::BBTCS_Multiline);
427+
CHECK_PARSE("BreakBeforeTemplateCloser: BlockIndent",
428+
BreakBeforeTemplateCloser, FormatStyle::BBTCS_BlockIndent);
429429
CHECK_PARSE("BreakBeforeTemplateCloser: Never", BreakBeforeTemplateCloser,
430430
FormatStyle::BBTCS_Never);
431431

clang/unittests/Format/FormatTest.cpp

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11134,7 +11134,7 @@ TEST_F(FormatTest, BreakBeforeTemplateCloser) {
1113411134
"void foo() {}",
1113511135
Style);
1113611136

11137-
Style.BreakBeforeTemplateCloser = FormatStyle::BBTCS_Multiline;
11137+
Style.BreakBeforeTemplateCloser = FormatStyle::BBTCS_BlockIndent;
1113811138
// BreakBeforeTemplateCloser should NOT force template declarations onto
1113911139
// multiple lines.
1114011140
verifyFormat("template <typename Foo>\n"
@@ -11156,6 +11156,10 @@ TEST_F(FormatTest, BreakBeforeTemplateCloser) {
1115611156
">\n"
1115711157
"void foo() {}",
1115811158
Style);
11159+
verifyNoChange("template <typename Foo,\n"
11160+
" typename Bar>\n"
11161+
"void foo() {}",
11162+
Style);
1115911163
// It should add a line break before > if not already present:
1116011164
verifyFormat("template <\n"
1116111165
" typename Foo\n"
@@ -11285,19 +11289,31 @@ TEST_F(FormatTest, BreakBeforeTemplateCloser) {
1128511289
">\n"
1128611290
"void foo() {}",
1128711291
Style);
11288-
// But not when the name is looong:
11292+
// But not when the name is looong. Note that these names are exactly 1
11293+
// character too long for the ColumnLimit.
1128911294
verifyFormat("template <\n"
1129011295
" typename Foo,\n"
11291-
" typename Barrrrrrrrrrrrrrrrrrrrrrrrrr\n"
11296+
" typename Barrrrrrrrrrrrrrrrrrrr\n"
1129211297
">\n"
1129311298
"void foo() {}",
1129411299
Style);
11300+
// Note that this "Foo" is 1 character shorter than the previous "Bar" because
11301+
// of the comma.
1129511302
verifyFormat("template <\n"
11296-
" typename Fooooooooooooooooooooooooooo,\n"
11303+
" typename Foooooooooooooooooooo,\n"
1129711304
" typename Bar\n"
1129811305
">\n"
1129911306
"void foo() {}",
1130011307
Style);
11308+
// BlockIndent style is used when the ColumnLimit allows it:
11309+
verifyFormat("template <typename Foo,\n"
11310+
" typename Barrrrrrrrrrrrrrrrrr>\n"
11311+
"void foo() {}",
11312+
Style);
11313+
verifyFormat("template <typename Fooooooooooooooooooo,\n"
11314+
" typename Bar>\n"
11315+
"void foo() {}",
11316+
Style);
1130111317
// Additionally, long names should be split in one step:
1130211318
verifyFormat(
1130311319
"template <\n"
@@ -11346,17 +11362,32 @@ TEST_F(FormatTest, BreakBeforeTemplateCloser) {
1134611362
" []<typename T,\n"
1134711363
" typename Loooooooooooooooooooooooooooooooooong\n"
1134811364
" >(T t) {};\n"
11365+
// Because this is not BlockIndent style, and the [] is empty,
11366+
// and the "T" is short, then the ">" is placed on the same line.
11367+
" auto lambda =\n"
11368+
" []<typename Loooooooooooooooooooooooooooooooooong,\n"
11369+
" typename T>(T t) {};\n"
1134911370
// Nested:
1135011371
" auto lambda =\n"
1135111372
" []<template <typename, typename>\n"
1135211373
" typename Looooooooooooooooooong\n"
1135311374
" >(T t) {};\n"
11354-
// Nested with long capture:
11375+
// Same idea, the "T" is now short rather than Looong:
11376+
" auto lambda =\n"
11377+
" []<template <typename, typename>\n"
11378+
" typename T>(T t) {};\n"
11379+
// Nested with long capture forces the style to block indent:
1135511380
" auto lambda =\n"
1135611381
" [loooooooooooooooooooong]<\n"
1135711382
" template <typename, typename>\n"
1135811383
" typename Looooooooooooooooooong\n"
1135911384
" >(T t) {};\n"
11385+
// But *now* it stays block indented even when T is short:
11386+
" auto lambda =\n"
11387+
" [loooooooooooooooooooong]<\n"
11388+
" template <typename, typename>\n"
11389+
" typename T\n"
11390+
" >(T t) {};\n"
1136011391
// Nested, with long name and long captures:
1136111392
" auto lambda =\n"
1136211393
" [loooooooooooooooooooong]<\n"
@@ -11366,6 +11397,13 @@ TEST_F(FormatTest, BreakBeforeTemplateCloser) {
1136611397
" >\n"
1136711398
" typename T\n"
1136811399
" >(T t) {};\n"
11400+
// Allow the nested template to be on the same line:
11401+
" auto lambda =\n"
11402+
" [loooooooooooooooooooong]<\n"
11403+
" template <typename Fooooooooo,\n"
11404+
" typename>\n"
11405+
" typename T\n"
11406+
" >(T t) {};\n"
1136911407
"}",
1137011408
Style);
1137111409
// Test that if the type is NOT long, it pulls it back into one line:
@@ -11387,6 +11425,12 @@ TEST_F(FormatTest, BreakBeforeTemplateCloser) {
1138711425
" >();\n"
1138811426
"}",
1138911427
Style);
11428+
// But if the type is short, we don't need block indent style:
11429+
verifyFormat("void foo() {\n"
11430+
" myFunc<Foo, Foo, Foo, Foo, Foo, Foo,\n"
11431+
" Foo, Foo>();\n"
11432+
"}",
11433+
Style);
1139011434
}
1139111435

1139211436
TEST_F(FormatTest, WrapsTemplateParameters) {

0 commit comments

Comments
 (0)