diff options
| author | Ben Hamilton <benhamilton@google.com> | 2018-04-03 14:07:11 +0000 |
|---|---|---|
| committer | Ben Hamilton <benhamilton@google.com> | 2018-04-03 14:07:11 +0000 |
| commit | 9dc781634680b4ceec095810653cdd68967d15f0 (patch) | |
| tree | 1693a7cc4758e0dec1c05832bed486edc49a9588 /clang/lib | |
| parent | 1915d2ac4f8d5b5bcfb716f7b03fb30d3228de82 (diff) | |
| download | bcm5719-llvm-9dc781634680b4ceec095810653cdd68967d15f0.tar.gz bcm5719-llvm-9dc781634680b4ceec095810653cdd68967d15f0.zip | |
[clang-format/ObjC] Do not detect "[]" as ObjC method expression
Summary:
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;
becomes:
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
https://github.com/llvm-mirror/clang/commit/b511fe9818829d7ece0cc0b2ce1fbe04a1f0739a
.
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
Fixes: https://bugs.llvm.org/show_bug.cgi?id=36248
Reviewers: djasper, jolesiak
Reviewed By: djasper
Subscribers: klimek, cfe-commits, djasper
Differential Revision: https://reviews.llvm.org/D45169
llvm-svn: 329070
Diffstat (limited to 'clang/lib')
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 038121e8ef8..cdf921c3c65 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -386,7 +386,7 @@ private: bool StartsObjCMethodExpr = !CppArrayTemplates && Style.isCpp() && !IsCpp11AttributeSpecifier && Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) && - CurrentToken->isNot(tok::l_brace) && + !CurrentToken->isOneOf(tok::l_brace, tok::r_square) && (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, tok::kw_return, tok::kw_throw) || |

