summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2014-11-11 19:34:57 +0000
committerDaniel Jasper <djasper@google.com>2014-11-11 19:34:57 +0000
commit64a328e96f926e6f751161f500245cd173674250 (patch)
tree174971e1744ce007aaaa1a46672e7d6b3f3a17c1
parent57e18ac96dc24540c4b512a3ad02a6255425e4a1 (diff)
downloadbcm5719-llvm-64a328e96f926e6f751161f500245cd173674250.tar.gz
bcm5719-llvm-64a328e96f926e6f751161f500245cd173674250.zip
clang-format: Preserve trailing-comma logic even with comments.
Before: vector<int> SomeVector = {// aaa 1, 2, }; After: vector<int> SomeVector = { // aaa 1, 2, }; llvm-svn: 221699
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp29
-rw-r--r--clang/unittests/Format/FormatTest.cpp13
2 files changed, 25 insertions, 17 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d3d4bb14fdb..715cbf0a483 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1775,9 +1775,22 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
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(const AnnotatedLine &Line,
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 &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 3485392d4fd..1f95a89893d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5645,9 +5645,9 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
// 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, LayoutCxx11BraceInitializers) {
" // 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;
OpenPOWER on IntegriCloud