Skip to content
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
83 changes: 63 additions & 20 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ the configuration (without a prefix: ``Auto``).
.. note::

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



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

.. _Cpp11BracedListStyle:

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

Important differences:
Possible values:

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

Fundamentally, C++11 braced lists are formatted exactly like function
calls would be formatted in their place. If the braced list follows a name
(e.g. a type or variable name), clang-format formats as if the ``{}`` were
the parentheses of a function call with that name. If there is no name,
a zero-length name is assumed.
* Spaces inside the braced list.
* Line break before the closing brace.
* Indentation with the block indent.


.. code-block:: c++

vector<int> x{ 1, 2, 3, 4 };
vector<T> x{ {}, {}, {}, {} };
f(MyMap[{ composite, key }]);
new int[3]{ 1, 2, 3 };
Type name{ // Comment
value
};

* ``BLS_FunctionCall`` (in configuration: ``FunctionCall``)
Best suited for C++11 braced lists.

* No spaces inside the braced list.
* No line break before the closing brace.
* Indentation with the continuation indent.

Fundamentally, C++11 braced lists are formatted exactly like function
calls would be formatted in their place. If the braced list follows a
name (e.g. a type or variable name), clang-format formats as if the
``{}`` were the parentheses of a function call with that name. If there
is no name, a zero-length name is assumed.

.. code-block:: c++

vector<int> x{1, 2, 3, 4};
vector<T> x{{}, {}, {}, {}};
f(MyMap[{composite, key}]);
new int[3]{1, 2, 3};
Type name{ // Comment
value};

* ``BLS_AlignFirstComment`` (in configuration: ``AlignFirstComment``)
Same as ``FunctionCall``, except for the handling of a comment at the
begin, it then aligns everything following with the comment.

* No spaces inside the braced list. (Even for a comment at the first
position.)
* No line break before the closing brace.
* Indentation with the continuation indent, except when followed by a
line comment, then it uses the block indent.


.. code-block:: c++

vector<int> x{1, 2, 3, 4};
vector<T> x{{}, {}, {}, {}};
f(MyMap[{composite, key}]);
new int[3]{1, 2, 3};
Type name{// Comment
value};

.. code-block:: c++

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

.. _DeriveLineEnding:

Expand Down Expand Up @@ -6625,7 +6668,7 @@ the configuration (without a prefix: ``Auto``).
.. note::

This option doesn't apply to initializer braces if
``Cpp11BracedListStyle`` is set to ``true``.
``Cpp11BracedListStyle`` is not ``Block``.

Possible values:

Expand Down
86 changes: 62 additions & 24 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct FormatStyle {
///
/// \note
/// This currently only applies to braced initializer lists (when
/// ``Cpp11BracedListStyle`` is ``true``) and parentheses.
/// ``Cpp11BracedListStyle`` is not ``Block``) and parentheses.
/// \endnote
BAS_BlockIndent,
};
Expand Down Expand Up @@ -2555,29 +2555,67 @@ struct FormatStyle {
/// \version 3.7
unsigned ContinuationIndentWidth;

/// If ``true``, format braced lists as best suited for C++11 braced
/// lists.
///
/// Important differences:
///
/// * No spaces inside the braced list.
/// * No line break before the closing brace.
/// * Indentation with the continuation indent, not with the block indent.
///
/// Fundamentally, C++11 braced lists are formatted exactly like function
/// calls would be formatted in their place. If the braced list follows a name
/// (e.g. a type or variable name), clang-format formats as if the ``{}`` were
/// the parentheses of a function call with that name. If there is no name,
/// a zero-length name is assumed.
/// \code
/// true: false:
/// vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
/// vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} };
/// f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]);
/// new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 };
/// \endcode
/// Different ways to handle braced lists.
enum BracedListStyle : int8_t {
/// Best suited for pre C++11 braced lists.
///
/// * Spaces inside the braced list.
/// * Line break before the closing brace.
/// * Indentation with the block indent.
///
/// \code
/// vector<int> x{ 1, 2, 3, 4 };
/// vector<T> x{ {}, {}, {}, {} };
/// f(MyMap[{ composite, key }]);
/// new int[3]{ 1, 2, 3 };
/// Type name{ // Comment
/// value
/// };
/// \endcode
BLS_Block,
/// Best suited for C++11 braced lists.
///
/// * No spaces inside the braced list.
/// * No line break before the closing brace.
/// * Indentation with the continuation indent.
///
/// Fundamentally, C++11 braced lists are formatted exactly like function
/// calls would be formatted in their place. If the braced list follows a
/// name (e.g. a type or variable name), clang-format formats as if the
/// ``{}`` were the parentheses of a function call with that name. If there
/// is no name, a zero-length name is assumed.
/// \code
/// vector<int> x{1, 2, 3, 4};
/// vector<T> x{{}, {}, {}, {}};
/// f(MyMap[{composite, key}]);
/// new int[3]{1, 2, 3};
/// Type name{ // Comment
/// value};
/// \endcode
BLS_FunctionCall,
/// Same as ``FunctionCall``, except for the handling of a comment at the
/// begin, it then aligns everything following with the comment.
///
/// * No spaces inside the braced list. (Even for a comment at the first
/// position.)
/// * No line break before the closing brace.
/// * Indentation with the continuation indent, except when followed by a
/// line comment, then it uses the block indent.
///
/// \code
/// vector<int> x{1, 2, 3, 4};
/// vector<T> x{{}, {}, {}, {}};
/// f(MyMap[{composite, key}]);
/// new int[3]{1, 2, 3};
/// Type name{// Comment
/// value};
/// \endcode
BLS_AlignFirstComment,
};

/// The style to handle braced lists.
/// \version 3.4
bool Cpp11BracedListStyle;
BracedListStyle Cpp11BracedListStyle;

/// This option is **deprecated**. See ``DeriveLF`` and ``DeriveCRLF`` of
/// ``LineEnding``.
Expand Down Expand Up @@ -4933,7 +4971,7 @@ struct FormatStyle {
/// Specifies when to insert a space in empty braces.
/// \note
/// This option doesn't apply to initializer braces if
/// ``Cpp11BracedListStyle`` is set to ``true``.
/// ``Cpp11BracedListStyle`` is not ``Block``.
/// \endnote
/// \version 22
SpaceInEmptyBracesStyle SpaceInEmptyBraces;
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Format/BreakableToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,10 @@ BreakableStringLiteralUsingOperators::BreakableStringLiteralUsingOperators(
// In Verilog, all strings are quoted by double quotes, joined by commas,
// and wrapped in braces. The comma is always before the newline.
assert(QuoteStyle == DoubleQuotes);
LeftBraceQuote = Style.Cpp11BracedListStyle ? "{\"" : "{ \"";
RightBraceQuote = Style.Cpp11BracedListStyle ? "\"}" : "\" }";
LeftBraceQuote =
Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? "{\"" : "{ \"";
RightBraceQuote =
Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? "\"}" : "\" }";
Postfix = "\",";
Prefix = "\"";
} else {
Expand Down
9 changes: 7 additions & 2 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
auto IsOpeningBracket = [&](const FormatToken &Tok) {
auto IsStartOfBracedList = [&]() {
return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
Style.Cpp11BracedListStyle;
Style.Cpp11BracedListStyle != FormatStyle::BLS_Block;
};
if (Tok.isNoneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
!IsStartOfBracedList()) {
Expand Down Expand Up @@ -925,7 +925,12 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
TT_TableGenDAGArgOpenerToBreak) &&
!(Current.MacroParent && Previous.MacroParent) &&
(Current.isNot(TT_LineComment) ||
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen)) &&
(Previous.is(BK_BracedInit) &&
(Style.Cpp11BracedListStyle != FormatStyle::BLS_FunctionCall ||
!Previous.Previous ||
Previous.Previous->isNoneOf(tok::identifier, tok::l_paren,
BK_BracedInit))) ||
Previous.is(TT_VerilogMultiLineListLParen)) &&
!IsInTemplateString(Current)) {
CurrentState.Indent = State.Column + Spaces;
CurrentState.IsAligned = true;
Expand Down
22 changes: 17 additions & 5 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ struct ScalarEnumerationTraits<FormatStyle::BreakTemplateDeclarationsStyle> {
}
};

template <> struct ScalarEnumerationTraits<FormatStyle::BracedListStyle> {
static void enumeration(IO &IO, FormatStyle::BracedListStyle &Value) {
IO.enumCase(Value, "Block", FormatStyle::BLS_Block);
IO.enumCase(Value, "FunctionCall", FormatStyle::BLS_FunctionCall);
IO.enumCase(Value, "AlignFirstComment", FormatStyle::BLS_AlignFirstComment);

// For backward compatibility.
IO.enumCase(Value, "false", FormatStyle::BLS_Block);
IO.enumCase(Value, "true", FormatStyle::BLS_AlignFirstComment);
}
};

template <> struct ScalarEnumerationTraits<FormatStyle::DAGArgStyle> {
static void enumeration(IO &IO, FormatStyle::DAGArgStyle &Value) {
IO.enumCase(Value, "DontBreak", FormatStyle::DAS_DontBreak);
Expand Down Expand Up @@ -1628,7 +1640,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.CompactNamespaces = false;
LLVMStyle.ConstructorInitializerIndentWidth = 4;
LLVMStyle.ContinuationIndentWidth = 4;
LLVMStyle.Cpp11BracedListStyle = true;
LLVMStyle.Cpp11BracedListStyle = FormatStyle::BLS_AlignFirstComment;
LLVMStyle.DerivePointerAlignment = false;
LLVMStyle.DisableFormat = false;
LLVMStyle.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
Expand Down Expand Up @@ -1904,7 +1916,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
// beneficial there. Investigate turning this on once proper string reflow
// has been implemented.
GoogleStyle.BreakStringLiterals = false;
GoogleStyle.Cpp11BracedListStyle = false;
GoogleStyle.Cpp11BracedListStyle = FormatStyle::BLS_Block;
GoogleStyle.SpacesInContainerLiterals = false;
} else if (Language == FormatStyle::LK_ObjC) {
GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
Expand Down Expand Up @@ -2000,7 +2012,7 @@ FormatStyle getMozillaStyle() {
MozillaStyle.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
MozillaStyle.ConstructorInitializerIndentWidth = 2;
MozillaStyle.ContinuationIndentWidth = 2;
MozillaStyle.Cpp11BracedListStyle = false;
MozillaStyle.Cpp11BracedListStyle = FormatStyle::BLS_Block;
MozillaStyle.FixNamespaceComments = false;
MozillaStyle.IndentCaseLabels = true;
MozillaStyle.ObjCSpaceAfterProperty = true;
Expand All @@ -2023,7 +2035,7 @@ FormatStyle getWebKitStyle() {
Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
Style.ColumnLimit = 0;
Style.Cpp11BracedListStyle = false;
Style.Cpp11BracedListStyle = FormatStyle::BLS_Block;
Style.FixNamespaceComments = false;
Style.IndentWidth = 4;
Style.NamespaceIndentation = FormatStyle::NI_Inner;
Expand All @@ -2043,7 +2055,7 @@ FormatStyle getGNUStyle() {
Style.BreakBeforeBraces = FormatStyle::BS_GNU;
Style.BreakBeforeTernaryOperators = true;
Style.ColumnLimit = 79;
Style.Cpp11BracedListStyle = false;
Style.Cpp11BracedListStyle = FormatStyle::BLS_Block;
Style.FixNamespaceComments = false;
Style.KeepFormFeed = true;
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
Expand Down
10 changes: 6 additions & 4 deletions clang/lib/Format/FormatToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const {
assert(is(tok::r_brace));
assert(MatchingParen);
assert(MatchingParen->is(tok::l_brace));
if (!Style.Cpp11BracedListStyle ||
if (Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent) {
return false;
}
Expand All @@ -88,7 +88,8 @@ bool FormatToken::opensBlockOrBlockTypeList(const FormatStyle &Style) const {
return is(TT_ArrayInitializerLSquare) || is(TT_ProtoExtensionLSquare) ||
(is(tok::l_brace) &&
(getBlockKind() == BK_Block || is(TT_DictLiteral) ||
(!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
(Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
NestingLevel == 0))) ||
(is(tok::less) && Style.isProto());
}

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