r173821 - Improve formatting of code with comments.
Daniel Jasper
djasper at google.com
Tue Jan 29 11:41:55 PST 2013
Author: djasper
Date: Tue Jan 29 13:41:55 2013
New Revision: 173821
URL: http://llvm.org/viewvc/llvm-project?rev=173821&view=rev
Log:
Improve formatting of code with comments.
Before:
aaaaaaa(aaaaaa( // comment
aaaaaaa));
<big mess>
After:
aaaaaaa(aaaaaa( // comment
aaaaaaaa));
function(/* parameter 1 */ aaaaaaa,
/* parameter 2 */ aaaaaaa,
/* parameter 3 */ aaaaaaa,
/* parameter 4 */ aaaaaaa);
(the latter example was only wrong in the one-arg-per-line mode, e.g. in
Google style).
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=173821&r1=173820&r2=173821&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Jan 29 13:41:55 2013
@@ -617,12 +617,11 @@ private:
(getPrecedence(Previous) == prec::Assignment ||
Previous.is(tok::kw_return)))
State.Stack.back().AssignmentColumn = State.Column + Spaces;
- if (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
- State.NextToken->Parent->Type == TT_TemplateOpener)
+ if (Current.Type != TT_LineComment &&
+ (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
+ State.NextToken->Parent->Type == TT_TemplateOpener))
State.Stack[ParenLevel].Indent = State.Column + Spaces;
- if (Current.getPreviousNoneComment() != NULL &&
- Current.getPreviousNoneComment()->is(tok::comma) &&
- Current.isNot(tok::comment))
+ if (Previous.is(tok::comma) && Current.Type != TT_LineComment)
State.Stack[ParenLevel].HasMultiParameterLine = true;
State.Column += Spaces;
@@ -746,7 +745,7 @@ private:
State.LineContainsContinuedForLoopSection)
return UINT_MAX;
if (!NewLine && State.NextToken->Parent->is(tok::comma) &&
- State.NextToken->isNot(tok::comment) &&
+ State.NextToken->Type != TT_LineComment &&
State.Stack.back().BreakAfterComma)
return UINT_MAX;
// Trying to insert a parameter on a new line if there are already more than
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=173821&r1=173820&r2=173821&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Jan 29 13:41:55 2013
@@ -447,6 +447,9 @@ TEST_F(FormatTest, UnderstandsSingleLine
format("int i;\\\n// single line trailing comment"));
verifyGoogleFormat("int a; // Trailing comment.");
+
+ verifyFormat("someFunction(anotherFunction( // Force break.\n"
+ " parameter));");
}
TEST_F(FormatTest, UnderstandsMultiLineComments) {
@@ -461,6 +464,11 @@ TEST_F(FormatTest, UnderstandsMultiLineC
" /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
"/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
+
+ verifyGoogleFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
+ " /* parameter 2 */ aaaaaa,\n"
+ " /* parameter 3 */ aaaaaa,\n"
+ " /* parameter 4 */ aaaaaa);");
}
TEST_F(FormatTest, CommentsInStaticInitializers) {
More information about the cfe-commits
mailing list