Skip to content

Commit cf28a47

Browse files
[clang-format] Remove special handling of comments after brace/paren (#71672)
Fixes http://llvm.org/PR55487 (#55487) The code did not match the documentation about Cpp11BracedListStyle. Changed handling of comments after opening braces, which are supposedly function call like to behave exactly like their parenthesis counter part.
1 parent 695cf01 commit cf28a47

15 files changed

+251
-79
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ the configuration (without a prefix: ``Auto``).
245245
.. note::
246246

247247
This currently only applies to braced initializer lists (when
248-
``Cpp11BracedListStyle`` is ``true``) and parentheses.
248+
``Cpp11BracedListStyle`` is not ``Block``) and parentheses.
249249

250250

251251

@@ -3816,29 +3816,72 @@ the configuration (without a prefix: ``Auto``).
38163816

38173817
.. _Cpp11BracedListStyle:
38183818

3819-
**Cpp11BracedListStyle** (``Boolean``) :versionbadge:`clang-format 3.4` :ref:`<Cpp11BracedListStyle>`
3820-
If ``true``, format braced lists as best suited for C++11 braced
3821-
lists.
3819+
**Cpp11BracedListStyle** (``BracedListStyle``) :versionbadge:`clang-format 3.4` :ref:`<Cpp11BracedListStyle>`
3820+
The style to handle braced lists.
38223821

3823-
Important differences:
3822+
Possible values:
38243823

3825-
* No spaces inside the braced list.
3826-
* No line break before the closing brace.
3827-
* Indentation with the continuation indent, not with the block indent.
3824+
* ``BLS_Block`` (in configuration: ``Block``)
3825+
Best suited for pre C++11 braced lists.
38283826

3829-
Fundamentally, C++11 braced lists are formatted exactly like function
3830-
calls would be formatted in their place. If the braced list follows a name
3831-
(e.g. a type or variable name), clang-format formats as if the ``{}`` were
3832-
the parentheses of a function call with that name. If there is no name,
3833-
a zero-length name is assumed.
3827+
* Spaces inside the braced list.
3828+
* Line break before the closing brace.
3829+
* Indentation with the block indent.
3830+
3831+
3832+
.. code-block:: c++
3833+
3834+
vector<int> x{ 1, 2, 3, 4 };
3835+
vector<T> x{ {}, {}, {}, {} };
3836+
f(MyMap[{ composite, key }]);
3837+
new int[3]{ 1, 2, 3 };
3838+
Type name{ // Comment
3839+
value
3840+
};
3841+
3842+
* ``BLS_FunctionCall`` (in configuration: ``FunctionCall``)
3843+
Best suited for C++11 braced lists.
3844+
3845+
* No spaces inside the braced list.
3846+
* No line break before the closing brace.
3847+
* Indentation with the continuation indent.
3848+
3849+
Fundamentally, C++11 braced lists are formatted exactly like function
3850+
calls would be formatted in their place. If the braced list follows a
3851+
name (e.g. a type or variable name), clang-format formats as if the
3852+
``{}`` were the parentheses of a function call with that name. If there
3853+
is no name, a zero-length name is assumed.
3854+
3855+
.. code-block:: c++
3856+
3857+
vector<int> x{1, 2, 3, 4};
3858+
vector<T> x{{}, {}, {}, {}};
3859+
f(MyMap[{composite, key}]);
3860+
new int[3]{1, 2, 3};
3861+
Type name{ // Comment
3862+
value};
3863+
3864+
* ``BLS_AlignFirstComment`` (in configuration: ``AlignFirstComment``)
3865+
Same as ``FunctionCall``, except for the handling of a comment at the
3866+
begin, it then aligns everything following with the comment.
3867+
3868+
* No spaces inside the braced list. (Even for a comment at the first
3869+
position.)
3870+
* No line break before the closing brace.
3871+
* Indentation with the continuation indent, except when followed by a
3872+
line comment, then it uses the block indent.
3873+
3874+
3875+
.. code-block:: c++
3876+
3877+
vector<int> x{1, 2, 3, 4};
3878+
vector<T> x{{}, {}, {}, {}};
3879+
f(MyMap[{composite, key}]);
3880+
new int[3]{1, 2, 3};
3881+
Type name{// Comment
3882+
value};
38343883

3835-
.. code-block:: c++
38363884

3837-
true: false:
3838-
vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
3839-
vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} };
3840-
f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]);
3841-
new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 };
38423885

38433886
.. _DeriveLineEnding:
38443887

@@ -6625,7 +6668,7 @@ the configuration (without a prefix: ``Auto``).
66256668
.. note::
66266669

66276670
This option doesn't apply to initializer braces if
6628-
``Cpp11BracedListStyle`` is set to ``true``.
6671+
``Cpp11BracedListStyle`` is not ``Block``.
66296672

66306673
Possible values:
66316674

clang/include/clang/Format/Format.h

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct FormatStyle {
9494
///
9595
/// \note
9696
/// This currently only applies to braced initializer lists (when
97-
/// ``Cpp11BracedListStyle`` is ``true``) and parentheses.
97+
/// ``Cpp11BracedListStyle`` is not ``Block``) and parentheses.
9898
/// \endnote
9999
BAS_BlockIndent,
100100
};
@@ -2555,29 +2555,67 @@ struct FormatStyle {
25552555
/// \version 3.7
25562556
unsigned ContinuationIndentWidth;
25572557

2558-
/// If ``true``, format braced lists as best suited for C++11 braced
2559-
/// lists.
2560-
///
2561-
/// Important differences:
2562-
///
2563-
/// * No spaces inside the braced list.
2564-
/// * No line break before the closing brace.
2565-
/// * Indentation with the continuation indent, not with the block indent.
2566-
///
2567-
/// Fundamentally, C++11 braced lists are formatted exactly like function
2568-
/// calls would be formatted in their place. If the braced list follows a name
2569-
/// (e.g. a type or variable name), clang-format formats as if the ``{}`` were
2570-
/// the parentheses of a function call with that name. If there is no name,
2571-
/// a zero-length name is assumed.
2572-
/// \code
2573-
/// true: false:
2574-
/// vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
2575-
/// vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} };
2576-
/// f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]);
2577-
/// new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 };
2578-
/// \endcode
2558+
/// Different ways to handle braced lists.
2559+
enum BracedListStyle : int8_t {
2560+
/// Best suited for pre C++11 braced lists.
2561+
///
2562+
/// * Spaces inside the braced list.
2563+
/// * Line break before the closing brace.
2564+
/// * Indentation with the block indent.
2565+
///
2566+
/// \code
2567+
/// vector<int> x{ 1, 2, 3, 4 };
2568+
/// vector<T> x{ {}, {}, {}, {} };
2569+
/// f(MyMap[{ composite, key }]);
2570+
/// new int[3]{ 1, 2, 3 };
2571+
/// Type name{ // Comment
2572+
/// value
2573+
/// };
2574+
/// \endcode
2575+
BLS_Block,
2576+
/// Best suited for C++11 braced lists.
2577+
///
2578+
/// * No spaces inside the braced list.
2579+
/// * No line break before the closing brace.
2580+
/// * Indentation with the continuation indent.
2581+
///
2582+
/// Fundamentally, C++11 braced lists are formatted exactly like function
2583+
/// calls would be formatted in their place. If the braced list follows a
2584+
/// name (e.g. a type or variable name), clang-format formats as if the
2585+
/// ``{}`` were the parentheses of a function call with that name. If there
2586+
/// is no name, a zero-length name is assumed.
2587+
/// \code
2588+
/// vector<int> x{1, 2, 3, 4};
2589+
/// vector<T> x{{}, {}, {}, {}};
2590+
/// f(MyMap[{composite, key}]);
2591+
/// new int[3]{1, 2, 3};
2592+
/// Type name{ // Comment
2593+
/// value};
2594+
/// \endcode
2595+
BLS_FunctionCall,
2596+
/// Same as ``FunctionCall``, except for the handling of a comment at the
2597+
/// begin, it then aligns everything following with the comment.
2598+
///
2599+
/// * No spaces inside the braced list. (Even for a comment at the first
2600+
/// position.)
2601+
/// * No line break before the closing brace.
2602+
/// * Indentation with the continuation indent, except when followed by a
2603+
/// line comment, then it uses the block indent.
2604+
///
2605+
/// \code
2606+
/// vector<int> x{1, 2, 3, 4};
2607+
/// vector<T> x{{}, {}, {}, {}};
2608+
/// f(MyMap[{composite, key}]);
2609+
/// new int[3]{1, 2, 3};
2610+
/// Type name{// Comment
2611+
/// value};
2612+
/// \endcode
2613+
BLS_AlignFirstComment,
2614+
};
2615+
2616+
/// The style to handle braced lists.
25792617
/// \version 3.4
2580-
bool Cpp11BracedListStyle;
2618+
BracedListStyle Cpp11BracedListStyle;
25812619

25822620
/// This option is **deprecated**. See ``DeriveLF`` and ``DeriveCRLF`` of
25832621
/// ``LineEnding``.
@@ -4933,7 +4971,7 @@ struct FormatStyle {
49334971
/// Specifies when to insert a space in empty braces.
49344972
/// \note
49354973
/// This option doesn't apply to initializer braces if
4936-
/// ``Cpp11BracedListStyle`` is set to ``true``.
4974+
/// ``Cpp11BracedListStyle`` is not ``Block``.
49374975
/// \endnote
49384976
/// \version 22
49394977
SpaceInEmptyBracesStyle SpaceInEmptyBraces;

clang/lib/Format/BreakableToken.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,10 @@ BreakableStringLiteralUsingOperators::BreakableStringLiteralUsingOperators(
306306
// In Verilog, all strings are quoted by double quotes, joined by commas,
307307
// and wrapped in braces. The comma is always before the newline.
308308
assert(QuoteStyle == DoubleQuotes);
309-
LeftBraceQuote = Style.Cpp11BracedListStyle ? "{\"" : "{ \"";
310-
RightBraceQuote = Style.Cpp11BracedListStyle ? "\"}" : "\" }";
309+
LeftBraceQuote =
310+
Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? "{\"" : "{ \"";
311+
RightBraceQuote =
312+
Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? "\"}" : "\" }";
311313
Postfix = "\",";
312314
Prefix = "\"";
313315
} else {

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
833833
auto IsOpeningBracket = [&](const FormatToken &Tok) {
834834
auto IsStartOfBracedList = [&]() {
835835
return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
836-
Style.Cpp11BracedListStyle;
836+
Style.Cpp11BracedListStyle != FormatStyle::BLS_Block;
837837
};
838838
if (Tok.isNoneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
839839
!IsStartOfBracedList()) {
@@ -925,7 +925,12 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
925925
TT_TableGenDAGArgOpenerToBreak) &&
926926
!(Current.MacroParent && Previous.MacroParent) &&
927927
(Current.isNot(TT_LineComment) ||
928-
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen)) &&
928+
(Previous.is(BK_BracedInit) &&
929+
(Style.Cpp11BracedListStyle != FormatStyle::BLS_FunctionCall ||
930+
!Previous.Previous ||
931+
Previous.Previous->isNoneOf(tok::identifier, tok::l_paren,
932+
BK_BracedInit))) ||
933+
Previous.is(TT_VerilogMultiLineListLParen)) &&
929934
!IsInTemplateString(Current)) {
930935
CurrentState.Indent = State.Column + Spaces;
931936
CurrentState.IsAligned = true;

clang/lib/Format/Format.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,18 @@ struct ScalarEnumerationTraits<FormatStyle::BreakTemplateDeclarationsStyle> {
304304
}
305305
};
306306

307+
template <> struct ScalarEnumerationTraits<FormatStyle::BracedListStyle> {
308+
static void enumeration(IO &IO, FormatStyle::BracedListStyle &Value) {
309+
IO.enumCase(Value, "Block", FormatStyle::BLS_Block);
310+
IO.enumCase(Value, "FunctionCall", FormatStyle::BLS_FunctionCall);
311+
IO.enumCase(Value, "AlignFirstComment", FormatStyle::BLS_AlignFirstComment);
312+
313+
// For backward compatibility.
314+
IO.enumCase(Value, "false", FormatStyle::BLS_Block);
315+
IO.enumCase(Value, "true", FormatStyle::BLS_AlignFirstComment);
316+
}
317+
};
318+
307319
template <> struct ScalarEnumerationTraits<FormatStyle::DAGArgStyle> {
308320
static void enumeration(IO &IO, FormatStyle::DAGArgStyle &Value) {
309321
IO.enumCase(Value, "DontBreak", FormatStyle::DAS_DontBreak);
@@ -1628,7 +1640,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16281640
LLVMStyle.CompactNamespaces = false;
16291641
LLVMStyle.ConstructorInitializerIndentWidth = 4;
16301642
LLVMStyle.ContinuationIndentWidth = 4;
1631-
LLVMStyle.Cpp11BracedListStyle = true;
1643+
LLVMStyle.Cpp11BracedListStyle = FormatStyle::BLS_AlignFirstComment;
16321644
LLVMStyle.DerivePointerAlignment = false;
16331645
LLVMStyle.DisableFormat = false;
16341646
LLVMStyle.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
@@ -1904,7 +1916,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
19041916
// beneficial there. Investigate turning this on once proper string reflow
19051917
// has been implemented.
19061918
GoogleStyle.BreakStringLiterals = false;
1907-
GoogleStyle.Cpp11BracedListStyle = false;
1919+
GoogleStyle.Cpp11BracedListStyle = FormatStyle::BLS_Block;
19081920
GoogleStyle.SpacesInContainerLiterals = false;
19091921
} else if (Language == FormatStyle::LK_ObjC) {
19101922
GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
@@ -2000,7 +2012,7 @@ FormatStyle getMozillaStyle() {
20002012
MozillaStyle.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
20012013
MozillaStyle.ConstructorInitializerIndentWidth = 2;
20022014
MozillaStyle.ContinuationIndentWidth = 2;
2003-
MozillaStyle.Cpp11BracedListStyle = false;
2015+
MozillaStyle.Cpp11BracedListStyle = FormatStyle::BLS_Block;
20042016
MozillaStyle.FixNamespaceComments = false;
20052017
MozillaStyle.IndentCaseLabels = true;
20062018
MozillaStyle.ObjCSpaceAfterProperty = true;
@@ -2023,7 +2035,7 @@ FormatStyle getWebKitStyle() {
20232035
Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
20242036
Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20252037
Style.ColumnLimit = 0;
2026-
Style.Cpp11BracedListStyle = false;
2038+
Style.Cpp11BracedListStyle = FormatStyle::BLS_Block;
20272039
Style.FixNamespaceComments = false;
20282040
Style.IndentWidth = 4;
20292041
Style.NamespaceIndentation = FormatStyle::NI_Inner;
@@ -2043,7 +2055,7 @@ FormatStyle getGNUStyle() {
20432055
Style.BreakBeforeBraces = FormatStyle::BS_GNU;
20442056
Style.BreakBeforeTernaryOperators = true;
20452057
Style.ColumnLimit = 79;
2046-
Style.Cpp11BracedListStyle = false;
2058+
Style.Cpp11BracedListStyle = FormatStyle::BLS_Block;
20472059
Style.FixNamespaceComments = false;
20482060
Style.KeepFormFeed = true;
20492061
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;

clang/lib/Format/FormatToken.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const {
6767
assert(is(tok::r_brace));
6868
assert(MatchingParen);
6969
assert(MatchingParen->is(tok::l_brace));
70-
if (!Style.Cpp11BracedListStyle ||
70+
if (Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
7171
Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent) {
7272
return false;
7373
}
@@ -88,7 +88,8 @@ bool FormatToken::opensBlockOrBlockTypeList(const FormatStyle &Style) const {
8888
return is(TT_ArrayInitializerLSquare) || is(TT_ProtoExtensionLSquare) ||
8989
(is(tok::l_brace) &&
9090
(getBlockKind() == BK_Block || is(TT_DictLiteral) ||
91-
(!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
91+
(Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
92+
NestingLevel == 0))) ||
9293
(is(tok::less) && Style.isProto());
9394
}
9495

@@ -184,7 +185,8 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
184185
// In C++11 braced list style, we should not format in columns unless they
185186
// have many items (20 or more) or we allow bin-packing of function call
186187
// arguments.
187-
if (Style.Cpp11BracedListStyle && !Style.BinPackArguments &&
188+
if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block &&
189+
!Style.BinPackArguments &&
188190
(Commas.size() < 19 || !Style.BinPackLongBracedList)) {
189191
return;
190192
}
@@ -228,7 +230,7 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
228230
ItemEnd = Token->MatchingParen;
229231
const FormatToken *NonCommentEnd = ItemEnd->getPreviousNonComment();
230232
ItemLengths.push_back(CodePointsBetween(ItemBegin, NonCommentEnd));
231-
if (Style.Cpp11BracedListStyle &&
233+
if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block &&
232234
!ItemEnd->Previous->isTrailingComment()) {
233235
// In Cpp11 braced list style, the } and possibly other subsequent
234236
// tokens will need to stay on a line with the last element.

0 commit comments

Comments
 (0)