This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] square parens that are followed by '=' are not Objective-C message sends
ClosedPublic

Authored by arphaman on Jan 2 2019, 3:24 PM.

Details

Summary

The commit r322690 introduced support for ObjC detection in header files. Unfortunately some C headers that use designated initializers are now incorrectly detected as Objective-C.
This patch fixes it by ensuring [ ... ] is not annotated as an Objective-C message send when the expression is followed by = as it's not a commonly used Objective-C pattern.

rdar://45504376

Diff Detail

Repository
rC Clang

Event Timeline

arphaman created this revision.Jan 2 2019, 3:24 PM

Hi Alex, thanks for fixing this.

I assume chance of testing an Objective-C header with assignment to method result without any other specifically Objective-C (detectable) construct is significantly lower than chance of testing a C99 header with designated initializers. In other words - having potential for false Objective-C negatives in parseSquare() is probably fine given the context. Is that right?

If my assumption is correct I'd say LGTM unless anyone says otherwise within 1 week.

Note that a message send needs to have two expressions without an operator in between. Can we also rule out all square brackets that just have a single identifier or number token?

I was thinking the same (that we might be able to make the detection more precise) but I got the impression these cases are handled later (L524 and below).

arphaman updated this revision to Diff 180110.Jan 3 2019, 11:24 AM

Do not annotate one token [ .... ] expressions as Objective-C message sends as suggested by Duncan.

benhamilton added inline comments.Jan 7 2019, 9:27 AM
lib/Format/TokenAnnotator.cpp
501

I think you need to check if CurrentToken->Previous is null before dereferencing it.

503

I think it's actually required to have more than one token. Can we reduce the change to that?

arphaman updated this revision to Diff 183372.Jan 24 2019, 12:47 PM
arphaman marked 3 inline comments as done.

Update for Ben's comments.

benhamilton accepted this revision.Jan 24 2019, 1:14 PM
This revision is now accepted and ready to land.Jan 24 2019, 1:14 PM
This revision was automatically updated to reflect the committed changes.