r186433 - Revamp the formatting of C++11 braced init lists.
Daniel Jasper
djasper at google.com
Tue Jul 16 11:22:10 PDT 2013
Author: djasper
Date: Tue Jul 16 13:22:10 2013
New Revision: 186433
URL: http://llvm.org/viewvc/llvm-project?rev=186433&view=rev
Log:
Revamp the formatting of C++11 braced init lists.
The fundamental concept is:
Format as if the braced init list was a function call (with parentheses
replaced by braces). If there is no name/type before the opening brace
(e.g. if the braced list is nested), assume a zero-length identifier
just before the opening brace.
This behavior is gated on a new style flag, which for now replaces the
SpacesInBracedLists style flag. Activate this style flag for Google
style to reflect recent style guide changes.
Modified:
cfe/trunk/include/clang/Format/Format.h
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=186433&r1=186432&r2=186433&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Tue Jul 16 13:22:10 2013
@@ -146,8 +146,20 @@ struct FormatStyle {
/// \brief The brace breaking style to use.
BraceBreakingStyle BreakBeforeBraces;
- /// \brief If \c true, format { 1 }, otherwise {1}.
- bool SpacesInBracedLists;
+ /// \brief If \c 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.
+ bool Cpp11BracedListStyle;
/// \brief If \c true, indent when breaking function declarations which
/// are not also definitions after the type.
@@ -183,7 +195,7 @@ struct FormatStyle {
PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine &&
PointerBindsToType == R.PointerBindsToType &&
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
- SpacesInBracedLists == R.SpacesInBracedLists &&
+ Cpp11BracedListStyle == R.Cpp11BracedListStyle &&
Standard == R.Standard && UseTab == R.UseTab &&
IndentFunctionDeclarationAfterType ==
R.IndentFunctionDeclarationAfterType;
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=186433&r1=186432&r2=186433&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Jul 16 13:22:10 2013
@@ -110,7 +110,7 @@ template <> struct MappingTraits<clang::
IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
IO.mapOptional("SpacesBeforeTrailingComments",
Style.SpacesBeforeTrailingComments);
- IO.mapOptional("SpacesInBracedLists", Style.SpacesInBracedLists);
+ IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
IO.mapOptional("Standard", Style.Standard);
IO.mapOptional("IndentWidth", Style.IndentWidth);
IO.mapOptional("UseTab", Style.UseTab);
@@ -151,7 +151,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
LLVMStyle.PointerBindsToType = false;
LLVMStyle.SpacesBeforeTrailingComments = 1;
- LLVMStyle.SpacesInBracedLists = true;
+ LLVMStyle.Cpp11BracedListStyle = false;
LLVMStyle.Standard = FormatStyle::LS_Cpp03;
LLVMStyle.IndentWidth = 2;
LLVMStyle.UseTab = false;
@@ -183,7 +183,7 @@ FormatStyle getGoogleStyle() {
GoogleStyle.ObjCSpaceBeforeProtocolList = false;
GoogleStyle.PointerBindsToType = true;
GoogleStyle.SpacesBeforeTrailingComments = 2;
- GoogleStyle.SpacesInBracedLists = false;
+ GoogleStyle.Cpp11BracedListStyle = true;
GoogleStyle.Standard = FormatStyle::LS_Auto;
GoogleStyle.IndentWidth = 2;
GoogleStyle.UseTab = false;
@@ -815,7 +815,8 @@ private:
unsigned LastSpace = State.Stack.back().LastSpace;
bool AvoidBinPacking;
if (Current.is(tok::l_brace)) {
- NewIndent = Style.IndentWidth + LastSpace;
+ NewIndent =
+ LastSpace + (Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth);
const FormatToken *NextNoComment = Current.getNextNonComment();
AvoidBinPacking = NextNoComment &&
NextNoComment->Type == TT_DesignatedInitializerPeriod;
@@ -1139,8 +1140,9 @@ private:
const FormatToken &Previous = *Current.Previous;
if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
return true;
- if (Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
- return true;
+ if (!Style.Cpp11BracedListStyle && Current.is(tok::r_brace) &&
+ State.Stack.back().BreakBeforeClosingBrace)
+ return true;
if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
return true;
if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=186433&r1=186432&r2=186433&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Jul 16 13:22:10 2013
@@ -1167,7 +1167,7 @@ bool TokenAnnotator::spaceRequiredBetwee
if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
return false; // No spaces in "{}".
if (Left.is(tok::l_brace) || Right.is(tok::r_brace))
- return Style.SpacesInBracedLists;
+ return !Style.Cpp11BracedListStyle;
if (Right.Type == TT_UnaryOperator)
return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
(Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=186433&r1=186432&r2=186433&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Jul 16 13:22:10 2013
@@ -1637,10 +1637,9 @@ TEST_F(FormatTest, NestedStaticInitializ
" { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
"};");
verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
- " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
- " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
- " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}\n"
- "};");
+ " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
+ " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
+ " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
verifyFormat(
"CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
" { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
@@ -1659,11 +1658,11 @@ TEST_F(FormatTest, NestedStaticInitializ
" 333333333333333333333333333333 } },\n"
" { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
verifyGoogleFormat(
- "SomeArrayOfSomeType a = {{{1, 2, 3}}, {{1, 2, 3}},\n"
- " {{111111111111111111111111111111,\n"
- " 222222222222222222222222222222,\n"
- " 333333333333333333333333333333}},\n"
- " {{1, 2, 3}}, {{1, 2, 3}}};");
+ "SomeArrayOfSomeType a = {\n"
+ " {{1, 2, 3}}, {{1, 2, 3}},\n"
+ " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
+ " 333333333333333333333333333333}},\n"
+ " {{1, 2, 3}}, {{1, 2, 3}}};");
// FIXME: We might at some point want to handle this similar to parameter
// lists, where we have an option to put each on a single line.
@@ -3902,7 +3901,7 @@ TEST_F(FormatTest, LayoutCxx11Constructo
" });");
FormatStyle NoSpaces = getLLVMStyle();
- NoSpaces.SpacesInBracedLists = false;
+ NoSpaces.Cpp11BracedListStyle = true;
verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces);
verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces);
verifyFormat("f({1, 2});", NoSpaces);
@@ -5307,7 +5306,7 @@ TEST_F(FormatTest, ParsesConfiguration)
CHECK_PARSE_BOOL(IndentCaseLabels);
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
CHECK_PARSE_BOOL(PointerBindsToType);
- CHECK_PARSE_BOOL(SpacesInBracedLists);
+ CHECK_PARSE_BOOL(Cpp11BracedListStyle);
CHECK_PARSE_BOOL(UseTab);
CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
More information about the cfe-commits
mailing list