r190039 - clang-format: Fix parsing and indenting lambdas.
Daniel Jasper
djasper at google.com
Thu Sep 5 03:04:31 PDT 2013
Author: djasper
Date: Thu Sep 5 05:04:31 2013
New Revision: 190039
URL: http://llvm.org/viewvc/llvm-project?rev=190039&view=rev
Log:
clang-format: Fix parsing and indenting lambdas.
Before:
void f() {
other(x.begin(), x.end(), //
[&](int, int) { return 1; });
}
After:
void f() {
other(x.begin(), x.end(), //
[&](int, int) { return 1; });
}
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.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=190039&r1=190038&r2=190039&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Thu Sep 5 05:04:31 2013
@@ -267,7 +267,8 @@ unsigned ContinuationIndenter::addTokenT
State.Column = State.Stack.back().Indent;
State.Stack.back().ColonPos = State.Column + Current.CodePointCount;
}
- } else if (Current.is(tok::l_square) && Current.Type != TT_ObjCMethodExpr) {
+ } else if (Current.is(tok::l_square) && Current.Type != TT_ObjCMethodExpr &&
+ Current.Type != TT_LambdaLSquare) {
if (State.Stack.back().StartOfArraySubscripts != 0)
State.Column = State.Stack.back().StartOfArraySubscripts;
else
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=190039&r1=190038&r2=190039&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Sep 5 05:04:31 2013
@@ -201,7 +201,8 @@ private:
while (CurrentToken != NULL) {
if (CurrentToken->is(tok::r_square)) {
- if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren)) {
+ if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
+ Left->Type == TT_ObjCMethodExpr) {
// An ObjC method call is rarely followed by an open parenthesis.
// FIXME: Do we incorrectly label ":" with this?
StartsObjCMethodExpr = false;
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=190039&r1=190038&r2=190039&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Thu Sep 5 05:04:31 2013
@@ -676,27 +676,22 @@ void UnwrappedLineParser::parseStructura
void UnwrappedLineParser::tryToParseLambda() {
assert(FormatTok->is(tok::l_square));
FormatToken &LSquare = *FormatTok;
- if (!tryToParseLambdaIntroducer()) {
+ if (!tryToParseLambdaIntroducer())
return;
- }
- if (FormatTok->is(tok::l_paren)) {
- parseParens();
- }
while (FormatTok->isNot(tok::l_brace)) {
switch (FormatTok->Tok.getKind()) {
- case tok::l_brace:
- break;
- case tok::l_paren:
- parseParens();
- break;
- case tok::semi:
- case tok::equal:
- case tok::eof:
- return;
- default:
- nextToken();
- break;
+ case tok::l_brace:
+ break;
+ case tok::l_paren:
+ parseParens();
+ break;
+ case tok::identifier:
+ case tok::kw_mutable:
+ nextToken();
+ break;
+ default:
+ return;
}
}
LSquare.Type = TT_LambdaLSquare;
@@ -707,23 +702,33 @@ bool UnwrappedLineParser::tryToParseLamb
nextToken();
if (FormatTok->is(tok::equal)) {
nextToken();
- if (FormatTok->is(tok::r_square)) return true;
- if (FormatTok->isNot(tok::comma)) return false;
+ if (FormatTok->is(tok::r_square)) {
+ nextToken();
+ return true;
+ }
+ if (FormatTok->isNot(tok::comma))
+ return false;
nextToken();
} else if (FormatTok->is(tok::amp)) {
nextToken();
- if (FormatTok->is(tok::r_square)) return true;
+ if (FormatTok->is(tok::r_square)) {
+ nextToken();
+ return true;
+ }
if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
return false;
}
- if (FormatTok->is(tok::comma)) nextToken();
+ if (FormatTok->is(tok::comma))
+ nextToken();
} else if (FormatTok->is(tok::r_square)) {
nextToken();
return true;
}
do {
- if (FormatTok->is(tok::amp)) nextToken();
- if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) return false;
+ if (FormatTok->is(tok::amp))
+ nextToken();
+ if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
+ return false;
nextToken();
if (FormatTok->is(tok::comma)) {
nextToken();
@@ -836,6 +841,9 @@ void UnwrappedLineParser::parseParens()
case tok::r_brace:
// A "}" inside parenthesis is an error if there wasn't a matching "{".
return;
+ case tok::l_square:
+ tryToParseLambda();
+ break;
case tok::l_brace: {
if (!tryToParseBracedList()) {
parseChildBlock();
@@ -1195,7 +1203,7 @@ static void printDebugInfo(const Unwrapp
for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
E = Line.Tokens.end();
I != E; ++I) {
- llvm::dbgs() << I->Tok->Tok.getName() << " ";
+ llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
}
for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
E = Line.Tokens.end();
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=190039&r1=190038&r2=190039&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Sep 5 05:04:31 2013
@@ -6295,12 +6295,10 @@ TEST_F(FormatTest, FormatsLambdas) {
verifyFormat("void f() {\n"
" other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
"}\n");
- // FIXME: The formatting is incorrect; this test currently checks that
- // parsing of the unwrapped lines doesn't regress.
verifyFormat("void f() {\n"
" other(x.begin(), //\n"
" x.end(), //\n"
- " [&](int, int) { return 1; });\n"
+ " [&](int, int) { return 1; });\n"
"}\n");
}
More information about the cfe-commits
mailing list