summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Format/FormatTestObjC.cpp
Commit message (Collapse)AuthorAgeFilesLines
* [clang-format] Add ability to wrap braces after multi-line control statementsPaul Hoad2019-10-031-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Change the BraceWrappingFlags' AfterControlStatement from a bool to an enum with three values: * "Never": This is the default, and does not do any brace wrapping after control statements. * "MultiLine": This only wraps braces after multi-line control statements (this really only happens when a ColumnLimit is specified). * "Always": This always wraps braces after control statements. The first and last options are backwards-compatible with "false" and "true", respectively. The new "MultiLine" option is useful for when a wrapped control statement's indentation matches the subsequent block's indentation. It makes it easier to see at a glance where the control statement ends and where the block's code begins. For example: ``` if ( foo && bar ) { baz(); } ``` vs. ``` if ( foo && bar ) { baz(); } ``` Short control statements (1 line) do not wrap the brace to the next line, e.g. ``` if (foo) { bar(); } else { baz(); } ``` Reviewers: sammccall, owenpan, reuk, MyDeveloperDay, klimek Reviewed By: MyDeveloperDay Subscribers: MyDeveloperDay, cfe-commits Patch By: mitchell-stellar Tags: #clang-format, #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D68296 llvm-svn: 373647
* Adds support for formatting NS_CLOSED_ENUM and CF_CLOSED_ENUM alongside ↵Ben Hamilton2019-07-221-1/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | NS_ENUM and CF_ENUM. Summary: Addresses the formatting of NS_CLOSED_ENUM and CF_CLOSED_ENUM, introduced in Swift 5. Before: ``` typedef NS_CLOSED_ENUM(NSInteger, Foo){FooValueOne = 1, FooValueTwo, FooValueThree}; ``` After: ``` typedef NS_CLOSED_ENUM(NSInteger, Foo) { FooValueOne = 1, FooValueTwo, FooValueThree }; ``` Contributed by heijink. Reviewers: benhamilton, krasimir Reviewed By: benhamilton Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65012 llvm-svn: 366719
* [Format/ObjC] Avoid breaking between unary operators and operandsBen Hamilton2019-07-191-0/+12
| | | | | | | | | | | | | | | | | | | | Summary: Test Plan: New tests added. Ran tests with: % ninja FormatTests && ./tools/clang/unittests/Format/FormatTests Confirmed tests failed before change and passed after change. Reviewers: krasimir, djasper, sammccall, klimek Reviewed By: sammccall Subscribers: klimek, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64775 llvm-svn: 366592
* [clang-format] Refine structured binding detectionKrasimir Georgiev2019-03-251-0/+24
| | | | | | | | | | | | | | | | | | | | | Summary: Revision r356575 had the unfortunate consequence that now clang-format never detects an ObjC call expression after `&&`. This patch tries harder to distinguish between C++17 structured bindings and ObjC call expressions and adds a few regression tests. Reviewers: klimek Reviewed By: klimek Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D59774 llvm-svn: 356928
* clang-format: distinguish ObjC call subexpressions after r355434Krasimir Georgiev2019-03-111-0/+28
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: The revision r355434 had the unfortunate side-effect that it started to recognize certain ObjC expressions with a call subexpression followed by a `a->b` subexpression as C++ lambda expressions. This patch adds a bit of logic to handle these cases and documents them in tests. The commented-out test cases in the new test suite are ones that were problematic before r355434. Reviewers: MyDeveloperDay, gribozavr Reviewed By: MyDeveloperDay, gribozavr Subscribers: MyDeveloperDay, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D59210 llvm-svn: 355831
* [Format/ObjC] Fix [foo bar]->baz formatting as lambda arrowBen Hamilton2019-02-081-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Currently, `UnwrappedLineParser` thinks an arrow token after an ObjC method expression is a C++ lambda arrow, so it formats: ``` [foo bar]->baz ``` as: ``` [foo bar] -> baz ``` Because `UnwrappedLineParser` runs before `TokenAnnotator`, it can't know if the arrow token is after an ObjC method expression or not. This diff makes `TokenAnnotator` remove the TT_LambdaArrow on the arrow token if it follows an ObjC method expression. Test Plan: New test added. Ran test with: % ninja FormatTests && ./tools/clang/unittests/Format/FormatTests Confirmed test failed before diff and passed after diff. Reviewers: krasimir, djasper, sammccall Reviewed By: sammccall Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D57923 llvm-svn: 353531
* [clang-format] square parens with one token are not Objective-C message sendsAlex Lorenz2019-01-241-0/+14
| | | | | | | | | | | | | | 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 commit fixes it by ensuring that `[ token ]` is not annotated as an Objective-C message send. rdar://45504376 Differential Revision: https://reviews.llvm.org/D56226 llvm-svn: 352125
* Update the file headers across all of the LLVM projects in the monorepoChandler Carruth2019-01-191-4/+3
| | | | | | | | | | | | | | | | | to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
* [clang-format] Fix BraceWrapping AfterFunction for ObjC methodsBen Hamilton2018-10-121-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: > clang-format --version > clang-format version 7.0.0 (tags/RELEASE_700/final) > echo "@implementation Foo\n- (void)foo:(id)bar\n{\n}\n@end\n" |clang-format -style='{BreakBeforeBraces: Custom, BraceWrapping: {AfterFunction: true}}' ``` @implementation Foo - (void)foo:(id)bar { } @end ``` with patch: > bin/clang-format --version > clang-format version 8.0.0 (trunk 344285) > echo "@implementation Foo\n- (void)foo:(id)bar\n{\n}\n@end\n" |bin/clang-format -style='{BreakBeforeBraces: Custom, BraceWrapping: {AfterFunction: true}}' ``` @implementation Foo - (void)foo:(id)bar { } @end ``` Contributed by hultman@. Reviewers: benhamilton, jolesiak, klimek, Wizard Reviewed By: benhamilton Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D53197 llvm-svn: 344406
* [clang-format/ObjC] Put ObjC method arguments into one line when they fitJacek Olesiak2018-07-091-0/+42
| | | | | | | | | | | | | | | | | | | | Reapply D47195: Currently BreakBeforeParameter is set to true everytime message receiver spans multiple lines, e.g.: ``` [[object block:^{ return 42; }] aa:42 bb:42]; ``` will be formatted: ``` [[object block:^{ return 42; }] aa:42 bb:42]; ``` even though arguments could fit into one line. This change fixes this behavior. llvm-svn: 336521
* [clang-format/ObjC] Improve split priorities for ObjC methodsJacek Olesiak2018-07-091-2/+19
| | | | | | | | | | Reduce penalty for aligning ObjC method arguments using the colon alignment as this is the canonical way. Trying to fit a whole expression into one line should not force other line breaks (e.g. when ObjC method expression is a part of other expression). llvm-svn: 336520
* [clang-format/ObjC] Fix NS_SWIFT_NAME(foo(bar:baz:)) after ObjC method declBen Hamilton2018-06-291-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: In D44638, I partially fixed `NS_SWIFT_NAME(foo(bar:baz:))`-style annotations on C functions, but didn't add a test for Objective-C method declarations. For ObjC method declarations which are annotated with `NS_SWIFT_NAME(...)`, we currently fail to annotate the final component of the selector name as `TT_SelectorName`. Because the token type is left unknown, clang-format will happily cause a compilation error when it changes the following: ``` @interface Foo - (void)doStuffWithFoo:(id)name bar:(id)bar baz:(id)baz NS_SWIFT_NAME(doStuff(withFoo:bar:baz:)); @end ``` to: ``` @interface Foo - (void)doStuffWithFoo:(id)name bar:(id)bar baz:(id)baz NS_SWIFT_NAME(doStuff(withFoo:bar:baz :)); @end ``` (note the linebreak before the final `:`). The logic which decides whether or not to annotate the token before a `:` with `TT_SelectorName` is pretty fragile, and has to handle some pretty odd cases like pair-parameters: ``` [I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd]; ``` So, to minimize the effect of this change, I decided to only annotate unknown identifiers before a `:` as `TT_SelectorName` for Objective-C declaration lines. Test Plan: New tests included. Confirmed tests failed before change and passed after change. Ran tests with: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, krasimir, jolesiak Reviewed By: krasimir Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D48679 llvm-svn: 335983
* [clang-format] Add AlwaysBreakBeforeMultilineString testsJacek Olesiak2018-06-221-0/+9
| | | | | | | | | | | | | | Summary: Followup to D47393. Reviewers: stephanemoore Reviewed By: stephanemoore Subscribers: benhamilton, cfe-commits Differential Revision: https://reviews.llvm.org/D48432 llvm-svn: 335338
* [clang-format] Disable AlwaysBreakBeforeMultilineStrings in Google style for ↵Ben Hamilton2018-06-141-0/+11
| | | | | | | | | | | | | | | | Objective-C 📜 Contributed by @stephanemoore. Reviewers: benhamilton, jolesiak, djasper Reviewed By: benhamilton Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D47393 llvm-svn: 334739
* [clang-format/ObjC] Correctly parse Objective-C methods with 'class' in nameBen Hamilton2018-05-301-1/+61
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Please take a close look at this CL. I haven't touched much of `UnwrappedLineParser` before, so I may have gotten things wrong. Previously, clang-format would incorrectly format the following: ``` @implementation Foo - (Class)class { } - (void)foo { } @end ``` as: ``` @implementation Foo - (Class)class { } - (void)foo { } @end ``` The problem is whenever `UnwrappedLineParser::parseStructuralElement()` sees any of the keywords `class`, `struct`, or `enum`, it calls `parseRecord()` to parse them as a C/C++ record. This causes subsequent lines to be parsed incorrectly, which causes them to be indented incorrectly. In Objective-C/Objective-C++, these keywords are valid selector components. This diff fixes the issue by explicitly handling `+` and `-` lines inside `@implementation` / `@interface` / `@protocol` blocks and parsing them as Objective-C methods. Test Plan: New tests added. Ran tests with: make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: jolesiak, klimek Reviewed By: jolesiak, klimek Subscribers: klimek, cfe-commits, Wizard Differential Revision: https://reviews.llvm.org/D47095 llvm-svn: 333553
* Revert "[clang-format] Fix putting ObjC message arguments in one line for ↵Jacek Olesiak2018-05-301-35/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | multiline receiver" Summary: This reverts commit db9e5e9a616d7fdd4d1ba4c3b2cd89d8a0238533 (rC333171). Mentioned change introduced unintended formatting of ObjC code due to split priorities inherited from C/C++, e.g.: ``` fooooooo = [ [obj fooo] aaa:42 aaa:42]; ``` instead of ``` fooooooo = [[obj fooo] aaa:42 aaa:42]; ``` when formatted with ColumnLimit = 30. Reviewers: krasimir Reviewed By: krasimir Subscribers: benhamilton, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D47527 llvm-svn: 333539
* [clang-format] Fix putting ObjC message arguments in one line for multiline ↵Jacek Olesiak2018-05-241-0/+35
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | receiver Summary: Reapply reverted changes from D46879. Currently BreakBeforeParameter is set to true everytime message receiver spans multiple lines, e.g.: ``` [[object block:^{ return 42; }] aa:42 bb:42]; ``` will be formatted: ``` [[object block:^{ return 42; }] aa:42 bb:42]; ``` even though arguments could fit into one line. This change fixes this behavior. Test Plan: make -j12 FormatTests && tools/clang/unittests/Format/FormatTests Reviewers: benhamilton, krasimir Reviewed By: benhamilton, krasimir Subscribers: djasper, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D47195 llvm-svn: 333171
* Revert "[clang-format] Fix putting ObjC message arguments in one line for ↵Jacek Olesiak2018-05-221-29/+0
| | | | | | | | | | | | | | | | multiline receiver" Summary: Reverts D46879 Reviewers: benhamilton Reviewed By: benhamilton Subscribers: krasimir, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D47205 llvm-svn: 332998
* [clang-format/ObjC] Correctly annotate single-component ObjC method invocationsBen Hamilton2018-05-181-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* [clang-format] Fix putting ObjC message arguments in one line for multiline ↵Jacek Olesiak2018-05-171-0/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | receiver Summary: Currently BreakBeforeParameter is set to true everytime message receiver spans multiple lines, e.g.: ``` [[object block:^{ return 42; }] aa:42 bb:42]; ``` will be formatted: ``` [[object block:^{ return 42; }] aa:42 bb:42]; ``` even though arguments could fit into one line. This change fixes this behavior. Test Plan: make -j12 FormatTests && tools/clang/unittests/Format/FormatTests Reviewers: benhamilton, djasper Reviewed By: benhamilton Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D46879 llvm-svn: 332582
* [clang] Update uses of DEBUG macro to LLVM_DEBUG.Nicola Zaghen2018-05-151-3/+3
| | | | | | | | | | | | | The DEBUG() macro is very generic so it might clash with other projects. The renaming was done as follows: - git grep -l 'DEBUG' | xargs sed -i 's/\bDEBUG\s\?(/LLVM_DEBUG(/g' - git diff -U0 master | ../clang/tools/clang-format/clang-format-diff.py -i -p1 -style LLVM Explicitly avoided changing the strings in the clang-format tests. Differential Revision: https://reviews.llvm.org/D44975 llvm-svn: 332350
* [clang-format] Continue after non-scope-closers in getLengthToMatchingParenKrasimir Georgiev2018-05-141-0/+12
| | | | | | | | | | | | | Summary: This fixes a regression introduced by `r331857` where we stop the search for the End token as soon as we hit a non-scope-closer, which prematurely stops before semicolons for example, which should otherwise be considered as part of the unbreakable tail. Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D46824 llvm-svn: 332225
* [clang-format/ObjC] Use getIdentifierInfo() instead of tok::identifierBen Hamilton2018-04-271-1/+12
| | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, we checked tokens for `tok::identifier` to see if they were identifiers inside an Objective-C selector. However, this missed C++ keywords like `new` and `delete`. To fix this, this diff uses `getIdentifierInfo()` to find identifiers or keywords inside Objective-C selectors. Test Plan: New tests added. Ran tests with: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D46143 llvm-svn: 331067
* [clang-format] Do not break after ObjC category open parenBen Hamilton2018-04-121-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, `clang-format` would break Objective-C category extensions after the opening parenthesis to avoid breaking the protocol list: ``` % echo "@interface ccccccccccccc (ccccccccccc) <ccccccccccccc> { }" | \ clang-format -assume-filename=foo.h -style="{BasedOnStyle: llvm, \ ColumnLimit: 40}" @interface ccccccccccccc ( ccccccccccc) <ccccccccccccc> { } ``` This looks fairly odd, as we could have kept the category extension on the previous line. Category extensions are a single item, so they are generally very short compared to protocol lists. We should prefer breaking after the opening `<` of the protocol list over breaking after the opening `(` of the category extension. With this diff, we now avoid breaking after the category extension's open paren, which causes us to break after the protocol list's open angle bracket: ``` % echo "@interface ccccccccccccc (ccccccccccc) <ccccccccccccc> { }" | \ ./bin/clang-format -assume-filename=foo.h -style="{BasedOnStyle: llvm, \ ColumnLimit: 40}" @interface ccccccccccccc (ccccccccccc) < ccccccccccccc> { } ``` Test Plan: New test added. Confirmed test failed before diff and passed after diff by running: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45526 llvm-svn: 329919
* [clang-format] Don't insert space between ObjC class and lightweight genericBen Hamilton2018-04-121-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: In D45185, I added clang-format parser support for Objective-C generics. However, I didn't touch the whitespace logic, so they got the same space logic as Objective-C protocol lists. In every example in the Apple SDK and in the documentation, there is no space between the class name and the opening `<` for the lightweight generic specification, so this diff removes the space and updates the tests. Test Plan: Tests updated. Ran tests with: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45498 llvm-svn: 329917
* [clang-format] Always indent wrapped Objective-C selector namesBen Hamilton2018-04-121-28/+9
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: Currently, indentation of Objective-C method names which are wrapped onto the next line due to a long return type is controlled by the style option `IndentWrappedFunctionNames`. This diff changes the behavior so we always indent wrapped Objective-C selector names. NOTE: I partially reverted https://github.com/llvm-mirror/clang/commit/6159c0fbd1876c7f5f984b4830c664cc78f16e2e / rL242484, as it was causing wrapped selectors to be double-indented. Its tests in FormatTestObjC.cpp still pass. Test Plan: Tests updated. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak, stephanemoore, thakis Reviewed By: djasper Subscribers: stephanemoore, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45004 llvm-svn: 329916
* [clang-format] Support lightweight Objective-C genericsBen Hamilton2018-04-051-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, `clang-format` didn't understand lightweight Objective-C generics, which have the form: ``` @interface Foo <KeyType, ValueTypeWithConstraint : Foo, AnotherValueTypeWithGenericConstraint: Bar<Baz>, ... > ... ``` The lightweight generic specifier list appears before the base class, if present, but because it starts with < like the protocol specifier list, `UnwrappedLineParser` was getting confused and failed to parse interfaces with both generics and protocol lists: ``` @interface Foo <KeyType> : NSObject <NSCopying> ``` Since the parsed line would be incomplete, the format result would be very confused (e.g., https://bugs.llvm.org/show_bug.cgi?id=24381). This fixes the issue by explicitly parsing the ObjC lightweight generic conformance list, so the line is fully parsed. Fixes: https://bugs.llvm.org/show_bug.cgi?id=24381 Test Plan: New tests added. Ran tests with: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45185 llvm-svn: 329298
* [clang-format] Ensure ObjC selectors with 0 args are annotated correctlyBen Hamilton2018-04-051-0/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, clang-format would incorrectly annotate 0-argument Objective-C selector names as TT_TrailingAnnotation: ``` % echo "-(void)foo;" > /tmp/test.m % ./bin/clang-format -debug /tmp/test.m Language: Objective-C ---- Line(0, FSC=0): minus[T=68, OC=0] l_paren[T=68, OC=1] void[T=68, OC=2] r_paren[T=68, OC=6] identifier[T=68, OC=7] semi[T=68, OC=10] Line(0, FSC=0): eof[T=68, OC=0] Run 0... AnnotatedTokens(L=0): M=0 C=0 T=ObjCMethodSpecifier S=1 B=0 BK=0 P=0 Name=minus L=1 PPK=2 FakeLParens= FakeRParens=0 Text='-' M=0 C=1 T=Unknown S=1 B=0 BK=0 P=33 Name=l_paren L=3 PPK=2 FakeLParens= FakeRParens=0 Text='(' M=0 C=1 T=Unknown S=0 B=0 BK=0 P=140 Name=void L=7 PPK=2 FakeLParens= FakeRParens=0 Text='void' M=0 C=0 T=CastRParen S=0 B=0 BK=0 P=43 Name=r_paren L=8 PPK=2 FakeLParens= FakeRParens=0 Text=')' M=0 C=1 T=TrailingAnnotation S=0 B=0 BK=0 P=120 Name=identifier L=11 PPK=2 FakeLParens= FakeRParens=0 Text='foo' M=0 C=0 T=Unknown S=0 B=0 BK=0 P=23 Name=semi L=12 PPK=2 FakeLParens= FakeRParens=0 Text=';' ``` This caused us to incorrectly indent 0-argument wrapped selectors when Style.IndentWrappedFunctionNames was false, as we thought the 0-argument ObjC selector name was actually a trailing annotation (which is always indented). This diff fixes the issue and adds tests. Test Plan: New tests added. Confirmed tests failed before diff. After diff, tests passed. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper, jolesiak Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44996 llvm-svn: 329297
* [clang-format] In tests, expected code should be format-stableMark Zeren2018-04-041-0/+1
| | | | | | | | | | | | | | | | | | | | | | Summary: Extend various verifyFormat helper functions to check that the expected text is "stable". This provides some protection against bugs where formatting results are ocilating between two forms, or continually change in some other way. Testing Done: * Ran unit tests. * Reproduced a known instability in preprocessor indentation which was caught by this new check. Reviewers: krasimir Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D42034 llvm-svn: 329231
* [clang-format/ObjC] Do not insert space after opening brace of ObjC dict literalBen Hamilton2018-04-031-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: D44816 attempted to fix a few cases where `clang-format` incorrectly inserted a space before the closing brace of an Objective-C dictionary literal. This revealed there were still a few cases where we inserted a space after the opening brace of an Objective-C dictionary literal. This fixes the formatting to be consistent and adds more tests. Test Plan: New tests added. Confirmed tests failed before diff and passed after diff. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak, krasimir Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45168 llvm-svn: 329069
* [clang-format] Ensure wrapped ObjC selectors with 1 arg obey ↵Ben Hamilton2018-03-301-0/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | IndentWrappedFunctionNames Summary: In D43121, @Typz introduced logic to avoid indenting 2-or-more argument ObjC selectors too far to the right if the first component of the selector was longer than the others. This had a small side effect of causing wrapped ObjC selectors with exactly 1 argument to not obey IndentWrappedFunctionNames: ``` - (aaaaaaaaaa) aaaaaaaaaa; ``` This diff fixes the issue by ensuring we align wrapped 1-argument ObjC selectors correctly: ``` - (aaaaaaaaaa) aaaaaaaaaa; ``` Test Plan: New tests added. Test failed before change, passed after change. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, klimek, Typz, jolesiak Reviewed By: djasper, jolesiak Subscribers: cfe-commits, Typz Differential Revision: https://reviews.llvm.org/D44994 llvm-svn: 328871
* [clang-format] Do not insert space before closing brace in ObjC dict literalBen Hamilton2018-03-271-0/+30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, `clang-format` would sometimes insert a space before the closing brace in an Objective-C dictionary literal. Unlike array literals (which obey `Style.SpacesInContainerLiterals` to add a space after `[` and before `]`), Objective-C dictionary literals currently are not meant to insert a space after `{` and before `}`, regardless of `Style.SpacesInContainerLiterals`. However, some constructs like `@{foo : @(bar)}` caused `clang-format` to insert a space between `)` and `}`. This fixes the issue and adds tests. (I understand the behavior is not consistent between array literals and dictionary literals, but that's existing behavior that's a much larger change.) Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak, Wizard Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44816 llvm-svn: 328627
* [clang-format] Don't insert space between r_paren and 'new' in ObjC declBen Hamilton2018-03-221-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, clang-format would insert a space between the closing parenthesis and 'new' in the following valid Objective-C declaration: + (instancetype)new; This was because 'new' is treated as a keyword, not an identifier. TokenAnnotator::spaceRequiredBefore() already handled the case where r_paren came before an identifier, so this diff extends it to handle r_paren before 'new'. Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak, stephanemoore Reviewed By: djasper, jolesiak, stephanemoore Subscribers: stephanemoore, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44692 llvm-svn: 328174
* [clang-format] Fix ObjC selectors with multiple params passed to macroBen Hamilton2018-03-201-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Objective-C selectors with arguments take the form of: foo: foo:bar: foo:bar:baz: These can be passed to a macro, like NS_SWIFT_NAME(): https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html and must never have spaces inserted around the colons. Previously, there was logic in TokenAnnotator's tok::colon parser to handle the single-argument case, but it failed for the multiple-argument cases. This diff fixes the bug and adds more tests. Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: jolesiak, djasper, Wizard Reviewed By: jolesiak, Wizard Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44638 llvm-svn: 327986
* [clang-format] Improve detection of Objective-C block typesBen Hamilton2018-03-121-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, clang-format would detect the following as an Objective-C block type: FOO(^); when it actually must be a C or C++ macro dealing with an XOR statement or an XOR operator overload. According to the Clang Block Language Spec: https://clang.llvm.org/docs/BlockLanguageSpec.html block types are of the form: int (^)(char, float) and block variables of block type are of the form: void (^blockReturningVoidWithVoidArgument)(void); int (^blockReturningIntWithIntAndCharArguments)(int, char); void (^arrayOfTenBlocksReturningVoidWithIntArgument[10])(int); This tightens up the detection so we don't unnecessarily detect C macros which pass in the XOR operator. Depends On D43904 Test Plan: New tests added. Ran tests with: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: krasimir, jolesiak, djasper Reviewed By: djasper Subscribers: djasper, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D43906 llvm-svn: 327285
* [clang-format] Improve detection of ObjC for-in statementsBen Hamilton2018-03-061-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, clang-format would detect the following as an Objective-C for-in statement: for (int x = in.value(); ...) {} because the logic only decided a for-loop was definitely *not* an Objective-C for-in loop after it saw a semicolon or a colon. To fix this, I delayed the decision of whether this was a for-in statement until after we found the matching right-paren, at which point we know if we've seen a semicolon or not. Test Plan: New tests added. Ran tests with: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: krasimir, jolesiak Reviewed By: jolesiak Subscribers: djasper, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D43904 llvm-svn: 326815
* clang-format: use AfterControlStatement to format ObjC control blocksFrancois Ferrand2018-02-271-2/+4
| | | | | | | | | | | | | | | | | | ObjC defines `@autoreleasepool` and `@synchronized` control blocks. These used to be formatted according to the `AfterObjCDeclaration` brace- wrapping flag, which is not very consistent. This patch changes the behavior to use the `AfterControlStatement` flag instead. This should not affect the behavior unless a custom brace wrapping mode is used. Reviewers: krasimir, djasper, klimek, benhamilton Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D43232 llvm-svn: 326192
* clang-format: fix formatting of ObjC @synchronized blocksFrancois Ferrand2018-02-271-0/+18
| | | | | | | | | | | | | | | | | | | | | | | Summary: The blocks used to be formatted using the "default" behavior, and would thus be mistaken for function calls followed by blocks: this could lead to unexpected inlining of the block and extra line-break before the opening brace. They are now formatted similarly to `@autoreleasepool` blocks, as expected: @synchronized(self) { f(); } Reviewers: krasimir, djasper, klimek Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D43114 llvm-svn: 326191
* [clang-format] Improve ObjC headers detectionJacek Olesiak2018-02-151-8/+17
| | | | | | | | | | | | | | Summary: Detect ObjC characteristic types when they start a line and add additional keywords. Reviewers: benhamilton Reviewed By: benhamilton Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D43124 llvm-svn: 325221
* clang-format: keep ObjC colon alignment with short object nameFrancois Ferrand2018-02-091-2/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: When the target object expression is short and the first selector name is long, clang-format used to break the colon alignment: [I performSelectorOnMainThread:@selector(loadAccessories) withObject:nil waitUntilDone:false]; This happens because the colon is placed at `ContinuationIndent + LongestObjCSelectorName`, so that any selector can be wrapped. This is however not needed in case the longest selector is the firstone, and not wrapped. To overcome this, this patch does not include the first selector in `LongestObjCSelectorName` computation (in TokenAnnotator), and lets `ContinuationIndenter` decide how to account for the first selector when wrapping. (Note this was already partly the case, see line 521 of ContinuationIndenter.cpp) This way, the code gets properly aligned whenever possible without breaking the continuation indent. [I performSelectorOnMainThread:@selector(loadAccessories) withObject:nil waitUntilDone:false]; [I // force break performSelectorOnMainThread:@selector(loadAccessories) withObject:nil waitUntilDone:false]; [I perform:@selector(loadAccessories) withSelectorOnMainThread:true waitUntilDone:false]; Reviewers: krasimir, djasper, klimek Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D43121 llvm-svn: 324741
* [clang-format] Do not break Objective-C string literals inside array literalsBen Hamilton2018-02-081-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Concatenating Objective-C string literals inside an array literal raises the warning -Wobjc-string-concatenation (which is enabled by default). clang-format currently splits and concatenates string literals like the following: NSArray *myArray = @[ @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ]; into: NSArray *myArray = @[ @"aaaaaaaaaaaaaaaaaaaaaaaaaaaa" @"aaaaaaaaa" ]; which raises the warning. This is https://bugs.llvm.org/show_bug.cgi?id=36153 . The options I can think of to fix this are: 1) Have clang-format disable Wobjc-string-concatenation by emitting pragmas around the formatted code 2) Have clang-format wrap the string literals in a macro (which disables the warning) 3) Disable string splitting for Objective-C string literals inside array literals I think 1) has no precedent, and I couldn't find a good identity() macro for 2). So, this diff implements 3). Test Plan: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: jolesiak, stephanemoore, djasper Reviewed By: jolesiak Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D42704 llvm-svn: 324618
* [clang-format] Set ObjCBinPackProtocolList to Never for google styleBen Hamilton2018-02-081-7/+10
| | | | | | | | | | | | | | | | | | | | Summary: This is split off from D42650, and sets ObjCBinPackProtocolList to Never for the google style. Depends On D42650 Test Plan: New tests added. make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: krasimir, jolesiak, stephanemoore Reviewed By: krasimir, jolesiak, stephanemoore Subscribers: klimek, cfe-commits, hokein, Wizard Differential Revision: https://reviews.llvm.org/D42708 llvm-svn: 324553
* [clang-format] Fix ObjC message arguments formatting.Jacek Olesiak2018-02-071-0/+33
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Fixes formatting of ObjC message arguments when inline block is a first argument. Having inline block as a first argument 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 Reviewers: krasimir, benhamilton Reviewed By: krasimir, benhamilton Subscribers: benhamilton, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D42493 llvm-svn: 324469
* [clang-format] Add more tests for Objective-C 2.0 generic alignmentBen Hamilton2018-02-061-0/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: In r236412, @djasper added a comment: // FIXME: We likely want to do this for more combinations of brackets. // Verify that it is wanted for ObjC, too. In D42650, @stephanemoore asked me to confirm this. This followup to D42650 adds more tests to verify the relative alignment behavior for Objective-C 2.0 generics passed to functions and removes the second half of the FIXME comment. Test Plan: make -j12 FormatTests && \ ./tools/clang/unittests/Format/FormatTests --gtest_filter=FormatTestObjC.\* Reviewers: stephanemoore, jolesiak, djasper Reviewed By: jolesiak Subscribers: klimek, cfe-commits, djasper, stephanemoore, krasimir Differential Revision: https://reviews.llvm.org/D42864 llvm-svn: 324364
* [clang-format] New format param ObjCBinPackProtocolListBen Hamilton2018-02-021-2/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This is an alternative approach to D42014 after some investigation by stephanemoore@ and myself. Previously, the format parameter `BinPackParameters` controlled both C function parameter list bin-packing and Objective-C protocol conformance list bin-packing. We found in the Google style, some teams were changing `BinPackParameters` from its default (`true`) to `false` so they could lay out Objective-C protocol conformance list items one-per-line instead of bin-packing them into as few lines as possible. To allow teams to use one-per-line Objective-C protocol lists without changing bin-packing for other areas like C function parameter lists, this diff introduces a new LibFormat parameter `ObjCBinPackProtocolList` to control the behavior just for ObjC protocol conformance lists. The new parameter is an enum which defaults to `Auto` to keep the previous behavior (delegating to `BinPackParameters`). Depends On D42649 Test Plan: New tests added. make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: jolesiak, stephanemoore, djasper Reviewed By: stephanemoore Subscribers: Wizard, hokein, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D42650 llvm-svn: 324131
* [clang-format] Add more tests for ObjC protocol list formatting behaviorBen Hamilton2018-01-291-0/+14
| | | | | | | | | | | | | | | | | | | | | | | Summary: The existing unit tests in FormatTestObjC.cpp didn't fully cover all the cases for protocol confirmance list formatting. This extends the unit tests to more cases of protocol conformance list formatting, especially how the behavior changes when `BinPackParameters` changes from `true` (the default) to `false`. Test Plan: make -j12 FormatTests && \ ./tools/clang/unittests/Format/FormatTests --gtest_filter=FormatTestObjC.\* Reviewers: krasimir, jolesiak, stephanemoore Reviewed By: krasimir Subscribers: benhamilton, klimek, cfe-commits, hokein, Wizard Differential Revision: https://reviews.llvm.org/D42649 llvm-svn: 323684
* clang-format: Support macros in front of @interface / @protocol for ObjC code.Nico Weber2018-01-231-0/+10
| | | | llvm-svn: 323226
* [ClangFormat] ObjCSpaceBeforeProtocolList should be true in the google styleBen Hamilton2018-01-181-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | Summary: The Google style guide is neutral on whether there should be a space before the protocol list in an Objective-C @interface or @implementation. The majority of Objective-C code in both Apple's public header files and Google's open-source uses a space before the protocol list, so this changes the google style to default ObjCSpaceBeforeProtocolList to true. Test Plan: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: krasimir, djasper, klimek Reviewed By: krasimir Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D41074 llvm-svn: 322873
* [Format] Improve ObjC header guessing heuristicBen Hamilton2018-01-171-0/+60
| | | | | | | | | | | | | | | | | | | | | Summary: This improves upon the previous Objective-C header guessing heuristic from rC320479. Now, we run the lexer on C++ header files and look for Objective-C keywords and syntax. We also look for Foundation types. Test Plan: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: jolesiak, krasimir Reviewed By: jolesiak Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D42135 llvm-svn: 322690
* Revert "[ClangFormat] IndentWrappedFunctionNames should be true in the ↵Ben Hamilton2017-12-141-5/+2
| | | | | | | | | | | google ObjC style" This reverts commit 37e69667f748e1458b46483b7c1b8f9ba33eec44. We're going to discuss its ramifications further before making a conclusion. llvm-svn: 320747
OpenPOWER on IntegriCloud