This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix ObjC message arguments formatting.
ClosedPublic

Authored by jolesiak on Jan 24 2018, 9:29 AM.

Details

Summary

Fixes formatting of ObjC message arguments when inline block is a first
argument.

Having inline block as a first when method has multiple parameters is
discouraged by Apple:
"It’s best practice to use only one block argument to a method. If the
method also needs other non-block arguments, the block should come last"
(https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html#//apple_ref/doc/uid/TP40011210-CH8-SW7),
it should be correctly formatted nevertheless.

Current formatting:

[object blockArgument:^{
  a = 42;
}
    anotherArg:42];

Fixed (colon alignment):

[object
  blockArgument:^{
    a = 42;
  }
     anotherArg:42];

Test Plan: make -j12 FormatTests && tools/clang/unittests/Format/FormatTests

Diff Detail

Repository
rL LLVM

Event Timeline

jolesiak created this revision.Jan 24 2018, 9:29 AM
jolesiak edited the summary of this revision. (Show Details)Jan 24 2018, 9:30 AM
jolesiak edited the summary of this revision. (Show Details)Jan 24 2018, 9:32 AM
jolesiak added a reviewer: krasimir.
jolesiak added a subscriber: benhamilton.
benhamilton accepted this revision.Jan 26 2018, 1:37 PM
benhamilton added inline comments.
lib/Format/TokenAnnotator.cpp
419 ↗(On Diff #131294)

What does this line do? Seems like it's initialized to 0 already, right?

This revision is now accepted and ready to land.Jan 26 2018, 1:37 PM
jolesiak added inline comments.Jan 29 2018, 2:03 AM
lib/Format/TokenAnnotator.cpp
419 ↗(On Diff #131294)

It is indeed initialized to 0.
However, before 'Left' bracket is recognized as TT_ObjCMethodExpr it has a different type assigned. Hence it gets updated here:
https://github.com/llvm-mirror/clang/blob/release_60/lib/Format/TokenAnnotator.cpp#L495
This assignment is due to fact that for other languages number of parameters is calculated as (1 + number_of_commas).

benhamilton added inline comments.Jan 29 2018, 9:06 AM
lib/Format/TokenAnnotator.cpp
419 ↗(On Diff #131294)

I see. Could you add a comment explaining that?

jolesiak updated this revision to Diff 131825.Jan 29 2018, 10:01 AM
  • Add comment explaining ParameterCount reset.
jolesiak marked an inline comment as done.Jan 29 2018, 10:02 AM
krasimir added inline comments.Jan 30 2018, 3:21 AM
unittests/Format/FormatTestObjC.cpp
678 ↗(On Diff #131825)

Can you put a test with a non-block between two blocks please

jolesiak updated this revision to Diff 131943.Jan 30 2018, 3:35 AM
  • Add test
jolesiak marked an inline comment as done.Jan 30 2018, 3:36 AM
krasimir accepted this revision.Jan 30 2018, 5:06 AM
This revision was automatically updated to reflect the committed changes.