From: Daniel Jasper Date: Wed, 9 Apr 2014 13:18:49 +0000 (+0000) Subject: clang-format: Treat a trailing comment like a trailing comma in braced lists. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0716e08cecfa9ecc2f70892dc011770d9674ed6a;p=clang clang-format: Treat a trailing comment like a trailing comma in braced lists. Before: static StructInitInfo module = {MODULE_BUILTIN, /* type */ "streams" /* name */ }; After: static StructInitInfo module = { MODULE_BUILTIN, /* type */ "streams" /* name */ }; This fixes llvm.org/PR19378. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205851 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index a0109713a6..df5603b268 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1546,14 +1546,16 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, return true; } - // 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. + // If the last token before a '}' is a comma or a 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->getPreviousNonComment(); + BeforeClosingBrace = Left.MatchingParen->Previous; else if (Right.is(tok::r_brace)) - BeforeClosingBrace = Right.getPreviousNonComment(); - if (BeforeClosingBrace && BeforeClosingBrace->is(tok::comma)) + BeforeClosingBrace = Right.Previous; + if (BeforeClosingBrace && + BeforeClosingBrace->isOneOf(tok::comma, tok::comment)) return true; return false; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 5f44c5bb2b..d7f1d0b483 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4516,8 +4516,9 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); verifyFormat( - "int *MyValues = {*A, // Operator detection might be confused by the '{'\n" - " *BB // Operator detection might be confused by previous comment\n" + "int *MyValues = {\n" + " *A, // Operator detection might be confused by the '{'\n" + " *BB // Operator detection might be confused by previous comment\n" "};"); verifyIndependentOfContext("if (int *a = &b)"); @@ -5072,7 +5073,10 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { " bbbbbbbbbbbbbbbbbbbb, bbbbb };", ExtraSpaces); verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); - verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", + verifyFormat("DoSomethingWithVector({\n" + " {} /* No data */\n" + " },\n" + " { { 1, 2 } });", ExtraSpaces); verifyFormat( "someFunction(OtherParam,\n"