Page MenuHomePhabricator

[clang-format/ObjC] Do not detect "[]" as ObjC method expression

Authored by benhamilton on Apr 2 2018, 9:30 AM.



The following C++ code was being detected by
guessLanguage() as Objective-C:

#define FOO(...) auto bar = [] __VA_ARGS__;

This was because [] __VA_ARGS__ is not currently detected as a C++
lambda expression (it has no parens or braces), so
TokenAnnotator::parseSquare() incorrectly treats the opening square
as an ObjC method expression.

We have two options to fix this:

  1. Parse [] __VA_ARGS__ explicitly as a C++ lambda
  2. Make it so [] is never parsed as an Objective-C method expression

This diff implements option 2, which causes the [ to be parsed
as TT_ArraySubscriptLSquare instead of TT_ObjCMethodExpr.

Note that when I fixed this, it caused one change in formatting
behavior, where the following was implicitly relying on the [
being parsed as TT_ObjCMethodExpr:

A<int * []> a;


A<int *[]> a;

with Style.PointerAlignment = Middle.

I don't really know what the desired format is for this syntax; the
test was added by Janusz Sobczak and integrated by @djasper in

I went ahead and changed the test for now.

Test Plan: New tests added. Ran tests with:

% make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests


Diff Detail


Event Timeline

benhamilton created this revision.Apr 2 2018, 9:30 AM
benhamilton edited the summary of this revision. (Show Details)Apr 2 2018, 10:47 AM
djasper accepted this revision.Apr 2 2018, 10:24 PM

Looks good. I like option 2 :).

This revision is now accepted and ready to land.Apr 2 2018, 10:24 PM
This revision was automatically updated to reflect the committed changes.