Skip to content

Commit 30b7d09

Browse files
committed
[Format/ObjC] Fix [foo bar]->baz formatting as lambda arrow
Summary: Currently, `UnwrappedLineParser` thinks an arrow token after an ObjC method expression is a C++ lambda arrow, so it formats: ``` [foo bar]->baz ``` as: ``` [foo bar] -> baz ``` Because `UnwrappedLineParser` runs before `TokenAnnotator`, it can't know if the arrow token is after an ObjC method expression or not. This diff makes `TokenAnnotator` remove the TT_LambdaArrow on the arrow token if it follows an ObjC method expression. Test Plan: New test added. Ran test with: % ninja FormatTests && ./tools/clang/unittests/Format/FormatTests Confirmed test failed before diff and passed after diff. Reviewers: krasimir, djasper, sammccall Reviewed By: sammccall Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D57923 llvm-svn: 353531
1 parent 494b8ac commit 30b7d09

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,10 @@ class AnnotatingParser {
520520
if (Parent && Parent->is(TT_PointerOrReference))
521521
Parent->Type = TT_BinaryOperator;
522522
}
523+
// An arrow after an ObjC method expression is not a lambda arrow.
524+
if (CurrentToken->Type == TT_ObjCMethodExpr && CurrentToken->Next &&
525+
CurrentToken->Next->is(TT_LambdaArrow))
526+
CurrentToken->Next->Type = TT_Unknown;
523527
Left->MatchingParen = CurrentToken;
524528
CurrentToken->MatchingParen = Left;
525529
// FirstObjCSelectorName is set when a colon is found. This does

clang/lib/Format/UnwrappedLineParser.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,9 @@ bool UnwrappedLineParser::tryToParseLambda() {
14261426
nextToken();
14271427
break;
14281428
case tok::arrow:
1429+
// This might or might not actually be a lambda arrow (this could be an
1430+
// ObjC method invocation followed by a dereferencing arrow). We might
1431+
// reset this back to TT_Unknown in TokenAnnotator.
14291432
FormatTok->Type = TT_LambdaArrow;
14301433
nextToken();
14311434
break;

clang/unittests/Format/FormatTestObjC.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
611611

612612
TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
613613
verifyFormat("[foo bar:baz];");
614+
verifyFormat("[foo bar]->baz;");
614615
verifyFormat("return [foo bar:baz];");
615616
verifyFormat("return (a)[foo bar:baz];");
616617
verifyFormat("f([foo bar:baz]);");

0 commit comments

Comments
 (0)