[cfe-commits] r172711 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp
Daniel Jasper
djasper at google.com
Thu Jan 17 04:53:34 PST 2013
Author: djasper
Date: Thu Jan 17 06:53:34 2013
New Revision: 172711
URL: http://llvm.org/viewvc/llvm-project?rev=172711&view=rev
Log:
Improve handling of comments in static initializers.
Also adding more tests.
We can now keep the formatting of something like:
static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */
aaaaaaaaaaaaaaaaaaaa /* comment */,
/* comment */ aaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaa, // comment
aaaaaaaaaaaaaaaaaaaa };
Note that the comment in the first line is handled like a trailing line comment
as that is likely what the user intended.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172711&r1=172710&r2=172711&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Jan 17 06:53:34 2013
@@ -98,6 +98,13 @@
std::vector<AnnotatedToken> Children;
AnnotatedToken *Parent;
+
+ const AnnotatedToken *getPreviousNoneComment() const {
+ AnnotatedToken *Tok = Parent;
+ while (Tok != NULL && Tok->is(tok::comment))
+ Tok = Tok->Parent;
+ return Tok;
+ }
};
class AnnotatedLine {
@@ -489,7 +496,8 @@
if (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
State.NextToken->Parent->Type == TT_TemplateOpener)
State.Stack[ParenLevel].Indent = State.Column + Spaces;
- if (Previous.is(tok::comma) && Current.Type != TT_LineComment)
+ if (Current.getPreviousNoneComment()->is(tok::comma) &&
+ Current.isNot(tok::comment))
State.Stack[ParenLevel].HasMultiParameterLine = true;
@@ -648,7 +656,7 @@
State.LineContainsContinuedForLoopSection)
return UINT_MAX;
if (!NewLine && State.NextToken->Parent->is(tok::comma) &&
- State.NextToken->Type != TT_LineComment &&
+ State.NextToken->isNot(tok::comment) &&
State.Stack.back().BreakAfterComma)
return UINT_MAX;
// Trying to insert a parameter on a new line if there are already more than
@@ -1033,7 +1041,8 @@
} else {
if (Current.Type == TT_LineComment) {
Current.MustBreakBefore = Current.FormatTok.NewlinesBefore > 0;
- } else if (Current.Parent->Type == TT_LineComment ||
+ } else if ((Current.Parent->is(tok::comment) &&
+ Current.FormatTok.NewlinesBefore > 0) ||
(Current.is(tok::string_literal) &&
Current.Parent->is(tok::string_literal))) {
Current.MustBreakBefore = true;
@@ -1376,7 +1385,7 @@
if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
return false;
- if (Right.is(tok::comment))
+ if (Right.Type == TT_LineComment)
// We rely on MustBreakBefore being set correctly here as we should not
// change the "binding" behavior of a comment.
return false;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172711&r1=172710&r2=172711&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jan 17 06:53:34 2013
@@ -359,6 +359,44 @@
TEST_F(FormatTest, UnderstandsMultiLineComments) {
verifyFormat("f(/*test=*/ true);");
+ EXPECT_EQ(
+ "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbb);",
+ format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
+ EXPECT_EQ(
+ "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
+ format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
+ "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
+}
+
+TEST_F(FormatTest, CommentsInStaticInitializers) {
+ EXPECT_EQ(
+ "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
+ " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
+ " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaa, // comment\n"
+ " aaaaaaaaaaaaaaaaaaaa };",
+ format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
+ " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
+ " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
+ " aaaaaaaaaaaaaaaaaaaa , // comment\n"
+ " aaaaaaaaaaaaaaaaaaaa };"));
+ verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
+ " bbbbbbbbbbb, ccccccccccc };");
+ verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
+ " // comment for bb....\n"
+ " bbbbbbbbbbb, ccccccccccc };");
+ verifyGoogleFormat(
+ "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
+ " bbbbbbbbbbb,\n"
+ " ccccccccccc };");
+ verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
+ " // comment for bb....\n"
+ " bbbbbbbbbbb,\n"
+ " ccccccccccc };");
+
}
//===----------------------------------------------------------------------===//
More information about the cfe-commits
mailing list