r204458 - clang-format: Let a trailing comma in braced lists enforce linebreaks.
Daniel Jasper
djasper at google.com
Fri Mar 21 05:38:57 PDT 2014
Author: djasper
Date: Fri Mar 21 07:38:57 2014
New Revision: 204458
URL: http://llvm.org/viewvc/llvm-project?rev=204458&view=rev
Log:
clang-format: Let a trailing comma in braced lists enforce linebreaks.
Before:
vector<int> x{1, 2, 3, 4, };
After:
vector<int> x{
1, 2, 3, 4,
};
This fixes llvm.org/PR18519.
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=204458&r1=204457&r2=204458&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Mar 21 07:38:57 2014
@@ -1478,6 +1478,7 @@ bool TokenAnnotator::spaceRequiredBefore
bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
const FormatToken &Right) {
+ const FormatToken &Left = *Right.Previous;
if (Right.is(tok::comment)) {
return Right.Previous->BlockKind != BK_BracedInit &&
Right.Previous->Type != TT_CtorInitializerColon &&
@@ -1514,6 +1515,13 @@ bool TokenAnnotator::mustBreakBefore(con
Style.Language == FormatStyle::LK_Proto) {
// Don't enums onto single lines in protocol buffers.
return true;
+ } else if ((Left.is(tok::l_brace) && Left.MatchingParen &&
+ Left.MatchingParen->Previous &&
+ Left.MatchingParen->Previous->is(tok::comma)) ||
+ (Right.is(tok::r_brace) && Left.is(tok::comma))) {
+ // If the last token before a '}' is a comma, the intention is to insert a
+ // line break after it in order to make shuffling around entries easier.
+ return true;
}
return false;
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=204458&r1=204457&r2=204458&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Mar 21 07:38:57 2014
@@ -4901,6 +4901,9 @@ TEST_F(FormatTest, LayoutBraceInitialize
TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
verifyFormat("vector<int> x{1, 2, 3, 4};");
+ verifyFormat("vector<int> x{\n"
+ " 1, 2, 3, 4,\n"
+ "};");
verifyFormat("vector<T> x{{}, {}, {}, {}};");
verifyFormat("f({1, 2});");
verifyFormat("auto v = Foo{-1};");
@@ -5036,8 +5039,9 @@ TEST_F(FormatTest, FormatsBracedListsInC
getLLVMStyleWithColumns(43));
// Trailing commas.
- verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
- " 1, 1, 1, 1, };",
+ verifyFormat("vector<int> x = {\n"
+ " 1, 1, 1, 1, 1, 1, 1, 1,\n"
+ "};",
getLLVMStyleWithColumns(39));
verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
" 1, 1, 1, 1, //\n"
@@ -6028,10 +6032,12 @@ TEST_F(FormatTest, ObjCDictLiterals) {
verifyFormat("@{}");
verifyFormat("@{@\"one\" : @1}");
verifyFormat("return @{@\"one\" : @1;");
- verifyFormat("@{@\"one\" : @1, }");
+ verifyFormat("@{@\"one\" : @1}");
verifyFormat("@{@\"one\" : @{@2 : @1}}");
- verifyFormat("@{@\"one\" : @{@2 : @1}, }");
+ verifyFormat("@{\n"
+ " @\"one\" : @{@2 : @1},\n"
+ "}");
verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
verifyFormat("[self setDict:@{}");
More information about the cfe-commits
mailing list