r221699 - clang-format: Preserve trailing-comma logic even with comments.
Daniel Jasper
djasper at google.com
Tue Nov 11 11:34:57 PST 2014
Author: djasper
Date: Tue Nov 11 13:34:57 2014
New Revision: 221699
URL: http://llvm.org/viewvc/llvm-project?rev=221699&view=rev
Log:
clang-format: Preserve trailing-comma logic even with comments.
Before:
vector<int> SomeVector = {// aaa
1, 2,
};
After:
vector<int> SomeVector = {
// aaa
1, 2,
};
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=221699&r1=221698&r2=221699&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Nov 11 13:34:57 2014
@@ -1775,9 +1775,22 @@ bool TokenAnnotator::mustBreakBefore(con
const FormatToken &Left = *Right.Previous;
if (Right.NewlinesBefore > 1)
return true;
+
+ // If the last token before a '}' is a comma or a trailing comment, the
+ // intention is to insert a line break after it in order to make shuffling
+ // around entries easier.
+ const FormatToken *BeforeClosingBrace = nullptr;
+ if (Left.is(tok::l_brace) && Left.BlockKind != BK_Block && Left.MatchingParen)
+ BeforeClosingBrace = Left.MatchingParen->Previous;
+ else if (Right.is(tok::r_brace) && Right.BlockKind != BK_Block)
+ BeforeClosingBrace = &Left;
+ if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
+ BeforeClosingBrace->isTrailingComment()))
+ return true;
+
if (Right.is(tok::comment)) {
- return Right.Previous->BlockKind != BK_BracedInit &&
- Right.Previous->Type != TT_CtorInitializerColon &&
+ return Left.BlockKind != BK_BracedInit &&
+ Left.Type != TT_CtorInitializerColon &&
(Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
} else if (Right.Previous->isTrailingComment() ||
(Right.isStringLiteral() && Right.Previous->isStringLiteral())) {
@@ -1826,18 +1839,6 @@ bool TokenAnnotator::mustBreakBefore(con
return true;
}
- // If the last token before a '}' is a comma or a trailing comment, the
- // intention is to insert a line break after it in order to make shuffling
- // around entries easier.
- const FormatToken *BeforeClosingBrace = nullptr;
- if (Left.is(tok::l_brace) && Left.MatchingParen)
- BeforeClosingBrace = Left.MatchingParen->Previous;
- else if (Right.is(tok::r_brace))
- BeforeClosingBrace = Right.Previous;
- if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
- BeforeClosingBrace->isTrailingComment()))
- return true;
-
if (Style.Language == FormatStyle::LK_JavaScript) {
// FIXME: This might apply to other languages and token kinds.
if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=221699&r1=221698&r2=221699&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Nov 11 13:34:57 2014
@@ -5645,9 +5645,9 @@ TEST_F(FormatTest, LayoutCxx11BraceIniti
// FIXME: The alignment of these trailing comments might be bad. Then again,
// this might be utterly useless in real code.
verifyFormat("Constructor::Constructor()\n"
- " : some_value{ //\n"
- " aaaaaaa //\n"
- " } {}");
+ " : some_value{ //\n"
+ " aaaaaaa, //\n"
+ " bbbbbbb} {}");
// In braced lists, the first comment is always assumed to belong to the
// first element. Thus, it can be moved to the next or previous line as
@@ -5671,6 +5671,13 @@ TEST_F(FormatTest, LayoutCxx11BraceIniti
" // Second element:\n"
" 2};",
getLLVMStyleWithColumns(30)));
+ // A trailing comma should still lead to an enforced line break.
+ EXPECT_EQ("vector<int> SomeVector = {\n"
+ " // aaa\n"
+ " 1, 2,\n"
+ "};",
+ format("vector<int> SomeVector = { // aaa\n"
+ " 1, 2, };"));
FormatStyle ExtraSpaces = getLLVMStyle();
ExtraSpaces.Cpp11BracedListStyle = false;
More information about the cfe-commits
mailing list