Skip to content

Commit 962b911

Browse files
committed
[clang-format/ObjC] Correctly annotate single-component ObjC method invocations
Summary: Previously, clang-format's parser would fail to annotate the selector in a single-component Objective-C method invocation with `TT_SelectorName`. For example, the following: [foo bar]; would parse `bar` as `TT_Unknown`: M=0 C=1 T=Unknown S=0 B=0 BK=0 P=140 Name=identifier L=34 PPK=2 FakeLParens= FakeRParens=0 II=0x559d5db51770 Text='bar' This caused us to fail to insert a space after a closing cast rparen, so the following: [((Foo *)foo) bar]; would format as: [((Foo *)foo)bar]; This diff fixes the issue by ensuring we annotate the selector in a single-component Objective-C method invocation as `TT_SelectorName`. Test Plan: New tests added. Ran tests with: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: jolesiak Subscribers: Wizard, klimek, hokein, cfe-commits Differential Revision: https://reviews.llvm.org/D47028 llvm-svn: 332727
1 parent 56e09c6 commit 962b911

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,12 @@ class AnnotatingParser {
501501
}
502502
if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
503503
CurrentToken->Type = TT_ObjCMethodExpr;
504+
// If we haven't seen a colon yet, make sure the last identifier
505+
// before the r_square is tagged as a selector name component.
506+
if (!ColonFound && CurrentToken->Previous &&
507+
CurrentToken->Previous->is(TT_Unknown) &&
508+
canBeObjCSelectorComponent(*CurrentToken->Previous))
509+
CurrentToken->Previous->Type = TT_SelectorName;
504510
// determineStarAmpUsage() thinks that '*' '[' is allocating an
505511
// array of pointers, but if '[' starts a selector then '*' is a
506512
// binary operator.

clang/unittests/Format/FormatTestObjC.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,10 @@ TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
792792
" a = 42;\n"
793793
" }];");
794794

795+
// Space between cast rparen and selector name component.
796+
verifyFormat("[((Foo *)foo) bar];");
797+
verifyFormat("[((Foo *)foo) bar:1 blech:2];");
798+
795799
// Message receiver taking multiple lines.
796800
Style.ColumnLimit = 20;
797801
// Non-corner case.

0 commit comments

Comments
 (0)