Skip to content

Commit f5e80d7

Browse files
[clang-format] Remove special handling of comments after brace/paren
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 023b1f6 commit f5e80d7

15 files changed

+255
-79
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ 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 ``AlignFirstComment`` or ``FunctionCall``)
249+
and parentheses.
249250

250251

251252

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

38173818
.. _Cpp11BracedListStyle:
38183819

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.
3820+
**Cpp11BracedListStyle** (``BracedListStyle``) :versionbadge:`clang-format 3.4` :ref:`<Cpp11BracedListStyle>`
3821+
The style to handle braced lists.
38223822

3823-
Important differences:
3823+
Possible values:
38243824

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.
3825+
* ``BLS_Block`` (in configuration: ``Block``)
3826+
Best suited for pre C++11 braced lists.
38283827

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

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

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 };
38423886

38433887
.. _DeriveLineEnding:
38443888

@@ -6625,7 +6669,8 @@ the configuration (without a prefix: ``Auto``).
66256669
.. note::
66266670

66276671
This option doesn't apply to initializer braces if
6628-
``Cpp11BracedListStyle`` is set to ``true``.
6672+
``Cpp11BracedListStyle`` is set to ``AlignFirstComment`` or
6673+
``FunctionCall``.
66296674

66306675
Possible values:
66316676

clang/include/clang/Format/Format.h

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ struct FormatStyle {
9494
///
9595
/// \note
9696
/// This currently only applies to braced initializer lists (when
97-
/// ``Cpp11BracedListStyle`` is ``true``) and parentheses.
97+
/// ``Cpp11BracedListStyle`` is ``AlignFirstComment`` or ``FunctionCall``)
98+
/// and parentheses.
9899
/// \endnote
99100
BAS_BlockIndent,
100101
};
@@ -2555,29 +2556,67 @@ struct FormatStyle {
25552556
/// \version 3.7
25562557
unsigned ContinuationIndentWidth;
25572558

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

25822621
/// This option is **deprecated**. See ``DeriveLF`` and ``DeriveCRLF`` of
25832622
/// ``LineEnding``.
@@ -4933,7 +4972,8 @@ struct FormatStyle {
49334972
/// Specifies when to insert a space in empty braces.
49344973
/// \note
49354974
/// This option doesn't apply to initializer braces if
4936-
/// ``Cpp11BracedListStyle`` is set to ``true``.
4975+
/// ``Cpp11BracedListStyle`` is set to ``AlignFirstComment`` or
4976+
/// ``FunctionCall``.
49374977
/// \endnote
49384978
/// \version 22
49394979
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)