r208175 - clang-format: Fix corner cases for comments in if conditions.
Daniel Jasper
djasper at google.com
Wed May 7 02:23:05 PDT 2014
Author: djasper
Date: Wed May 7 04:23:05 2014
New Revision: 208175
URL: http://llvm.org/viewvc/llvm-project?rev=208175&view=rev
Log:
clang-format: Fix corner cases for comments in if conditions.
Before:
if ( // a
x + 3) { ..
After:
if ( // a
x + 3) { ..
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=208175&r1=208174&r2=208175&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Wed May 7 04:23:05 2014
@@ -290,10 +290,11 @@ void ContinuationIndenter::addTokenOnCur
State.Stack.back().ContainsUnwrappedBuilder = true;
State.Column += Spaces;
- if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
+ if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
+ Previous.Previous && Previous.Previous->isOneOf(tok::kw_if, tok::kw_for))
// Treat the condition inside an if as if it was a second function
// parameter, i.e. let nested calls have a continuation indent.
- State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
+ State.Stack.back().LastSpace = State.Column;
else if (Current.isNot(tok::comment) &&
(Previous.is(tok::comma) ||
(Previous.is(tok::colon) && Previous.Type == TT_ObjCMethodExpr)))
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=208175&r1=208174&r2=208175&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed May 7 04:23:05 2014
@@ -1667,7 +1667,7 @@ bool TokenAnnotator::canBreakBefore(cons
return false;
if (Left.is(tok::l_paren) && Left.Previous &&
(Left.Previous->Type == TT_BinaryOperator ||
- Left.Previous->Type == TT_CastRParen))
+ Left.Previous->Type == TT_CastRParen || Left.Previous->is(tok::kw_if)))
return false;
if (Right.Type == TT_ImplicitStringLiteral)
return false;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=208175&r1=208174&r2=208175&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed May 7 04:23:05 2014
@@ -390,6 +390,11 @@ TEST_F(FormatTest, ElseIf) {
"else {\n"
" g()\n"
"}");
+
+ verifyFormat("if (a) {\n"
+ "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
+ "}");
}
TEST_F(FormatTest, FormatsForLoop) {
@@ -847,6 +852,18 @@ TEST_F(FormatTest, UnderstandsSingleLine
" int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
" int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
getLLVMStyleWithColumns(61));
+
+ verifyFormat("if ( // This is some comment\n"
+ " x + 3) {\n"
+ "}");
+ EXPECT_EQ("if ( // This is some comment\n"
+ " // spanning two lines\n"
+ " x + 3) {\n"
+ "}",
+ format("if( // This is some comment\n"
+ " // spanning two lines\n"
+ " x + 3) {\n"
+ "}"));
}
TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
More information about the cfe-commits
mailing list