xref: /llvm-project/clang/unittests/Format/FormatTestObjC.cpp (revision 63d6659a045d2e014d53ae890eec8b3cd96e5805)
1 //===- unittest/Format/FormatTestObjC.cpp - Formatting unit tests----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FormatTestBase.h"
10 
11 #define DEBUG_TYPE "format-test-objc"
12 
13 namespace clang {
14 namespace format {
15 namespace test {
16 namespace {
17 
18 class FormatTestObjC : public FormatTestBase {
19 protected:
20   FormatTestObjC() {
21     Style = getLLVMStyle();
22     Style.Language = FormatStyle::LK_ObjC;
23   }
24 
25   FormatStyle getDefaultStyle() const override { return Style; }
26 
27   FormatStyle Style;
28 };
29 
30 #define verifyIncompleteFormat(...)                                            \
31   _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
32 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
33 
34 TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
35   auto Style = getStyle("LLVM", "a.h", "none",
36                         "@interface\n"
37                         "- (id)init;");
38   ASSERT_TRUE((bool)Style);
39   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
40 
41   Style = getStyle("LLVM", "a.h", "none",
42                    "@interface\n"
43                    "+ (id)init;");
44   ASSERT_TRUE((bool)Style);
45   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
46 
47   Style = getStyle("LLVM", "a.h", "none",
48                    "@interface\n"
49                    "@end\n"
50                    "//comment");
51   ASSERT_TRUE((bool)Style);
52   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
53 
54   Style = getStyle("LLVM", "a.h", "none",
55                    "@interface\n"
56                    "@end //comment");
57   ASSERT_TRUE((bool)Style);
58   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
59 
60   // No recognizable ObjC.
61   Style = getStyle("LLVM", "a.h", "none", "void f() {}");
62   ASSERT_TRUE((bool)Style);
63   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
64 
65   Style = getStyle("{}", "a.h", "none", "@interface Foo\n@end\n");
66   ASSERT_TRUE((bool)Style);
67   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
68 
69   Style = getStyle("{}", "a.h", "none",
70                    "const int interface = 1;\nconst int end = 2;\n");
71   ASSERT_TRUE((bool)Style);
72   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
73 
74   Style = getStyle("{}", "a.h", "none", "@protocol Foo\n@end\n");
75   ASSERT_TRUE((bool)Style);
76   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
77 
78   Style = getStyle("{}", "a.h", "none",
79                    "const int protocol = 1;\nconst int end = 2;\n");
80   ASSERT_TRUE((bool)Style);
81   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
82 
83   Style = getStyle("{}", "a.h", "none", "typedef NS_ENUM(int, Foo) {};\n");
84   ASSERT_TRUE((bool)Style);
85   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
86 
87   Style =
88       getStyle("{}", "a.h", "none", "typedef NS_CLOSED_ENUM(int, Foo) {};\n");
89   ASSERT_TRUE((bool)Style);
90   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
91 
92   Style =
93       getStyle("{}", "a.h", "none", "typedef NS_ERROR_ENUM(int, Foo) {};\n");
94   ASSERT_TRUE((bool)Style);
95   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
96 
97   Style = getStyle("{}", "a.h", "none", R"objc(
98 NS_ASSUME_NONNULL_BEGIN
99 extern int i;
100 NS_ASSUME_NONNULL_END
101 )objc");
102   ASSERT_TRUE((bool)Style);
103   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
104 
105   Style = getStyle("{}", "a.h", "none", R"objc(
106 FOUNDATION_EXTERN void DoStuff(void);
107 )objc");
108   ASSERT_TRUE((bool)Style);
109   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
110 
111   Style = getStyle("{}", "a.h", "none", R"objc(
112 FOUNDATION_EXPORT void DoStuff(void);
113 )objc");
114   ASSERT_TRUE((bool)Style);
115   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
116 
117   Style = getStyle("{}", "a.h", "none", "enum Foo {};");
118   ASSERT_TRUE((bool)Style);
119   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
120 
121   Style =
122       getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n");
123   ASSERT_TRUE((bool)Style);
124   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
125 
126   Style =
127       getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }\n");
128   ASSERT_TRUE((bool)Style);
129   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
130 
131   Style =
132       getStyle("{}", "a.h", "none", "inline void Foo() { id = @[1, 2, 3]; }\n");
133   ASSERT_TRUE((bool)Style);
134   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
135 
136   Style = getStyle("{}", "a.h", "none",
137                    "inline void Foo() { id foo = @{1: 2, 3: 4, 5: 6}; }\n");
138   ASSERT_TRUE((bool)Style);
139   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
140 
141   Style = getStyle("{}", "a.h", "none",
142                    "inline void Foo() { int foo[] = {1, 2, 3}; }\n");
143   ASSERT_TRUE((bool)Style);
144   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
145 
146   // ObjC characteristic types.
147   Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
148   ASSERT_TRUE((bool)Style);
149   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
150 
151   Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
152   ASSERT_TRUE((bool)Style);
153   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
154 
155   Style = getStyle("{}", "a.h", "none", "NSObject *Foo();\n");
156   ASSERT_TRUE((bool)Style);
157   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
158 
159   Style = getStyle("{}", "a.h", "none", "NSSet *Foo();\n");
160   ASSERT_TRUE((bool)Style);
161   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
162 }
163 
164 TEST(FormatTestObjCStyle, AvoidDetectingDesignatedInitializersAsObjCInHeaders) {
165   auto Style = getStyle("LLVM", "a.h", "none",
166                         "static const char *names[] = {[0] = \"foo\",\n"
167                         "[kBar] = \"bar\"};");
168   ASSERT_TRUE((bool)Style);
169   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
170 
171   Style = getStyle("LLVM", "a.h", "none",
172                    "static const char *names[] = {[0] EQ \"foo\",\n"
173                    "[kBar] EQ \"bar\"};");
174   ASSERT_TRUE((bool)Style);
175   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
176 }
177 
178 TEST_F(FormatTestObjC, FormatObjCTryCatch) {
179   verifyFormat("@try {\n"
180                "  f();\n"
181                "} @catch (NSException e) {\n"
182                "  @throw;\n"
183                "} @finally {\n"
184                "  exit(42);\n"
185                "}");
186   verifyFormat("DEBUG({\n"
187                "  @try {\n"
188                "  } @finally {\n"
189                "  }\n"
190                "});\n");
191 }
192 
193 TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
194   verifyFormat("@autoreleasepool {\n"
195                "  f();\n"
196                "}\n"
197                "@autoreleasepool {\n"
198                "  f();\n"
199                "}\n");
200   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
201   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
202   verifyFormat("@autoreleasepool\n"
203                "{\n"
204                "  f();\n"
205                "}\n"
206                "@autoreleasepool\n"
207                "{\n"
208                "  f();\n"
209                "}\n");
210 }
211 
212 TEST_F(FormatTestObjC, FormatObjCGenerics) {
213   Style.ColumnLimit = 40;
214   verifyFormat("int aaaaaaaaaaaaaaaa(\n"
215                "    NSArray<aaaaaaaaaaaaaaaaaa *>\n"
216                "        aaaaaaaaaaaaaaaaa);\n");
217   verifyFormat("int aaaaaaaaaaaaaaaa(\n"
218                "    NSArray<aaaaaaaaaaaaaaaaaaa<\n"
219                "        aaaaaaaaaaaaaaaa *> *>\n"
220                "        aaaaaaaaaaaaaaaaa);\n");
221 }
222 
223 TEST_F(FormatTestObjC, FormatObjCSynchronized) {
224   verifyFormat("@synchronized(self) {\n"
225                "  f();\n"
226                "}\n"
227                "@synchronized(self) {\n"
228                "  f();\n"
229                "}\n");
230   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
231   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
232   verifyFormat("@synchronized(self)\n"
233                "{\n"
234                "  f();\n"
235                "}\n"
236                "@synchronized(self)\n"
237                "{\n"
238                "  f();\n"
239                "}\n");
240 }
241 
242 TEST_F(FormatTestObjC, FormatObjCInterface) {
243   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
244                "@public\n"
245                "  int field1;\n"
246                "@protected\n"
247                "  int field2;\n"
248                "@private\n"
249                "  int field3;\n"
250                "@package\n"
251                "  int field4;\n"
252                "}\n"
253                "+ (id)init;\n"
254                "@end");
255 
256   verifyFormat("@interface /* wait for it */ Foo\n"
257                "+ (id)init;\n"
258                "// Look, a comment!\n"
259                "- (int)answerWith:(int)i;\n"
260                "@end");
261 
262   verifyFormat("@interface Foo\n"
263                "@end\n"
264                "@interface Bar\n"
265                "@end");
266 
267   verifyFormat("@interface Foo : Bar\n"
268                "@property(assign, readwrite) NSInteger bar;\n"
269                "+ (id)init;\n"
270                "@end");
271 
272   verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n"
273                "@property(assign, readwrite) NSInteger bar;\n"
274                "+ (id)init;\n"
275                "@end");
276 
277   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
278                "+ (id)init;\n"
279                "@end");
280 
281   verifyFormat("@interface Foo (HackStuff)\n"
282                "+ (id)init;\n"
283                "@end");
284 
285   verifyFormat("@interface Foo ()\n"
286                "+ (id)init;\n"
287                "@end");
288 
289   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
290                "+ (id)init;\n"
291                "@end");
292 
293   verifyFormat("@interface Foo {\n"
294                "  int _i;\n"
295                "}\n"
296                "+ (id)init;\n"
297                "@end");
298 
299   verifyFormat("@interface Foo : Bar {\n"
300                "  int _i;\n"
301                "}\n"
302                "+ (id)init;\n"
303                "@end");
304 
305   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
306                "  int _i;\n"
307                "}\n"
308                "+ (id)init;\n"
309                "@end");
310 
311   verifyFormat("@interface Foo<Baz : Blech> : Bar <Baz, Quux> {\n"
312                "  int _i;\n"
313                "}\n"
314                "+ (id)init;\n"
315                "@end");
316 
317   verifyFormat("@interface Foo<Bar : Baz <Blech>> : Xyzzy <Corge> {\n"
318                "  int _i;\n"
319                "}\n"
320                "+ (id)init;\n"
321                "@end");
322 
323   verifyFormat("@interface Foo<Bar : Baz <Blech>> : Xyzzy <Corge> <Quux> {\n"
324                "  int _i;\n"
325                "}\n"
326                "+ (id)init;\n"
327                "@end");
328 
329   verifyFormat("@interface Foo : Bar <Baz> <Blech>\n"
330                "@end");
331 
332   verifyFormat("@interface Foo : Bar <Baz> <Blech, Xyzzy, Corge>\n"
333                "@end");
334 
335   verifyFormat("@interface Foo (HackStuff) {\n"
336                "  int _i;\n"
337                "}\n"
338                "+ (id)init;\n"
339                "@end");
340 
341   verifyFormat("@interface Foo () {\n"
342                "  int _i;\n"
343                "}\n"
344                "+ (id)init;\n"
345                "@end");
346 
347   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
348                "  int _i;\n"
349                "}\n"
350                "+ (id)init;\n"
351                "@end");
352   verifyFormat("@interface Foo\n"
353                "- (void)foo {\n"
354                "}\n"
355                "@end\n"
356                "@implementation Bar\n"
357                "- (void)bar {\n"
358                "}\n"
359                "@end");
360   Style.ColumnLimit = 40;
361   verifyFormat("@interface ccccccccccccc () <\n"
362                "    ccccccccccccc, ccccccccccccc,\n"
363                "    ccccccccccccc, ccccccccccccc> {\n"
364                "}");
365   verifyFormat("@interface ccccccccccccc (ccccccccccc) <\n"
366                "    ccccccccccccc> {\n"
367                "}");
368   Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
369   verifyFormat("@interface ddddddddddddd () <\n"
370                "    ddddddddddddd,\n"
371                "    ddddddddddddd,\n"
372                "    ddddddddddddd,\n"
373                "    ddddddddddddd> {\n"
374                "}");
375 
376   Style.BinPackParameters = false;
377   Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
378   verifyFormat("@interface eeeeeeeeeeeee () <\n"
379                "    eeeeeeeeeeeee,\n"
380                "    eeeeeeeeeeeee,\n"
381                "    eeeeeeeeeeeee,\n"
382                "    eeeeeeeeeeeee> {\n"
383                "}");
384   Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always;
385   verifyFormat("@interface fffffffffffff () <\n"
386                "    fffffffffffff, fffffffffffff,\n"
387                "    fffffffffffff, fffffffffffff> {\n"
388                "}");
389 
390   Style = getGoogleStyle(FormatStyle::LK_ObjC);
391   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
392                " @public\n"
393                "  int field1;\n"
394                " @protected\n"
395                "  int field2;\n"
396                " @private\n"
397                "  int field3;\n"
398                " @package\n"
399                "  int field4;\n"
400                "}\n"
401                "+ (id)init;\n"
402                "@end");
403   verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
404                "+ (id)init;\n"
405                "@end");
406   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
407                "+ (id)init;\n"
408                "@end");
409   Style.ColumnLimit = 40;
410   // BinPackParameters should be true by default.
411   verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
412                "              int eeeee, int eeeee);\n");
413   // ObjCBinPackProtocolList should be BPS_Never by default.
414   verifyFormat("@interface fffffffffffff () <\n"
415                "    fffffffffffff,\n"
416                "    fffffffffffff,\n"
417                "    fffffffffffff,\n"
418                "    fffffffffffff> {\n"
419                "}");
420   verifyFormat("@interface ggggggggggggg\n"
421                "    : ggggggggggggg <ggggggggggggg>\n"
422                "      <ggggggggggggg>\n"
423                "@end");
424 }
425 
426 TEST_F(FormatTestObjC, FormatObjCImplementation) {
427   verifyFormat("@implementation Foo : NSObject {\n"
428                "@public\n"
429                "  int field1;\n"
430                "@protected\n"
431                "  int field2;\n"
432                "@private\n"
433                "  int field3;\n"
434                "@package\n"
435                "  int field4;\n"
436                "}\n"
437                "+ (id)init {\n}\n"
438                "@end");
439 
440   verifyFormat("@implementation Foo\n"
441                "+ (id)init {\n"
442                "  if (true)\n"
443                "    return nil;\n"
444                "}\n"
445                "// Look, a comment!\n"
446                "- (int)answerWith:(int)i {\n"
447                "  return i;\n"
448                "}\n"
449                "+ (int)answerWith:(int)i {\n"
450                "  return i;\n"
451                "}\n"
452                "@end");
453 
454   verifyFormat("@implementation Foo\n"
455                "@end\n"
456                "@implementation Bar\n"
457                "@end");
458 
459   EXPECT_EQ("@implementation Foo : Bar\n"
460             "+ (id)init {\n}\n"
461             "- (void)foo {\n}\n"
462             "@end",
463             format("@implementation Foo : Bar\n"
464                    "+(id)init{}\n"
465                    "-(void)foo{}\n"
466                    "@end"));
467 
468   verifyFormat("@implementation Foo {\n"
469                "  int _i;\n"
470                "}\n"
471                "+ (id)init {\n}\n"
472                "@end");
473 
474   verifyFormat("@implementation Foo : Bar {\n"
475                "  int _i;\n"
476                "}\n"
477                "+ (id)init {\n}\n"
478                "@end");
479 
480   verifyFormat("@implementation Foo (HackStuff)\n"
481                "+ (id)init {\n}\n"
482                "@end");
483   verifyFormat("@implementation ObjcClass\n"
484                "- (void)method;\n"
485                "{}\n"
486                "@end");
487 
488   Style = getGoogleStyle(FormatStyle::LK_ObjC);
489   verifyFormat("@implementation Foo : NSObject {\n"
490                " @public\n"
491                "  int field1;\n"
492                " @protected\n"
493                "  int field2;\n"
494                " @private\n"
495                "  int field3;\n"
496                " @package\n"
497                "  int field4;\n"
498                "}\n"
499                "+ (id)init {\n}\n"
500                "@end");
501 }
502 
503 TEST_F(FormatTestObjC, FormatObjCProtocol) {
504   verifyFormat("@protocol Foo\n"
505                "@property(weak) id delegate;\n"
506                "- (NSUInteger)numberOfThings;\n"
507                "@end");
508 
509   verifyFormat("@protocol MyProtocol <NSObject>\n"
510                "- (NSUInteger)numberOfThings;\n"
511                "@end");
512 
513   verifyFormat("@protocol Foo;\n"
514                "@protocol Bar;\n");
515 
516   verifyFormat("@protocol Foo\n"
517                "@end\n"
518                "@protocol Bar\n"
519                "@end");
520 
521   verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
522                "@property(assign, readwrite) NSInteger bar;\n"
523                "@end");
524 
525   verifyFormat("@protocol myProtocol\n"
526                "- (void)mandatoryWithInt:(int)i;\n"
527                "@optional\n"
528                "- (void)optional;\n"
529                "@required\n"
530                "- (void)required;\n"
531                "@optional\n"
532                "@property(assign) int madProp;\n"
533                "@end\n");
534 
535   verifyFormat("@property(nonatomic, assign, readonly)\n"
536                "    int *looooooooooooooooooooooooooooongNumber;\n"
537                "@property(nonatomic, assign, readonly)\n"
538                "    NSString *looooooooooooooooooooooooooooongName;");
539 
540   verifyFormat("@implementation PR18406\n"
541                "}\n"
542                "@end");
543 
544   Style = getGoogleStyle(FormatStyle::LK_ObjC);
545   verifyFormat("@protocol MyProtocol <NSObject>\n"
546                "- (NSUInteger)numberOfThings;\n"
547                "@end");
548 }
549 
550 TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
551   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
552                "                   rect:(NSRect)theRect\n"
553                "               interval:(float)theInterval {\n"
554                "}");
555   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
556                "      longKeyword:(NSRect)theRect\n"
557                "    longerKeyword:(float)theInterval\n"
558                "            error:(NSError **)theError {\n"
559                "}");
560   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
561                "          longKeyword:(NSRect)theRect\n"
562                "    evenLongerKeyword:(float)theInterval\n"
563                "                error:(NSError **)theError {\n"
564                "}");
565   verifyFormat("+ (instancetype)new;\n");
566   Style.ColumnLimit = 60;
567   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
568                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
569                "    NS_DESIGNATED_INITIALIZER;");
570   verifyFormat("- (void)drawRectOn:(id)surface\n"
571                "            ofSize:(size_t)height\n"
572                "                  :(size_t)width;");
573   Style.ColumnLimit = 40;
574   // Make sure selectors with 0, 1, or more arguments are indented when wrapped.
575   verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
576                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n");
577   verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
578                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
579   verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
580                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
581                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
582   verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
583                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
584                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
585   verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
586                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
587                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
588 
589   // Continuation indent width should win over aligning colons if the function
590   // name is long.
591   Style = getGoogleStyle(FormatStyle::LK_ObjC);
592   Style.ColumnLimit = 40;
593   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
594                "    dontAlignNamef:(NSRect)theRect {\n"
595                "}");
596 
597   // Make sure we don't break aligning for short parameter names.
598   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
599                "       aShortf:(NSRect)theRect {\n"
600                "}");
601 
602   // Format pairs correctly.
603   Style.ColumnLimit = 80;
604   verifyFormat("- (void)drawRectOn:(id)surface\n"
605                "            ofSize:(aaaaaaaa)height\n"
606                "                  :(size_t)width\n"
607                "          atOrigin:(size_t)x\n"
608                "                  :(size_t)y\n"
609                "             aaaaa:(a)yyy\n"
610                "               bbb:(d)cccc;");
611   verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
612 
613   // BraceWrapping AfterFunction is respected for ObjC methods
614   Style = getGoogleStyle(FormatStyle::LK_ObjC);
615   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
616   Style.BraceWrapping.AfterFunction = true;
617   verifyFormat("@implementation Foo\n"
618                "- (void)foo:(id)bar\n"
619                "{\n"
620                "}\n"
621                "@end\n");
622 }
623 
624 TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
625   verifyFormat("[foo bar:baz];");
626   verifyFormat("[foo bar]->baz;");
627   verifyFormat("return [foo bar:baz];");
628   verifyFormat("return (a)[foo bar:baz];");
629   verifyFormat("f([foo bar:baz]);");
630   verifyFormat("f(2, [foo bar:baz]);");
631   verifyFormat("f(2, a ? b : c);");
632   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
633 
634   // Unary operators.
635   verifyFormat("int a = +[foo bar:baz];");
636   verifyFormat("int a = -[foo bar:baz];");
637   verifyFormat("int a = ![foo bar:baz];");
638   verifyFormat("int a = ~[foo bar:baz];");
639   verifyFormat("int a = ++[foo bar:baz];");
640   verifyFormat("int a = --[foo bar:baz];");
641   verifyFormat("int a = sizeof [foo bar:baz];");
642   verifyFormat("int a = alignof [foo bar:baz];");
643   verifyFormat("int a = &[foo bar:baz];");
644   verifyFormat("int a = *[foo bar:baz];");
645   // FIXME: Make casts work, without breaking f()[4].
646   // verifyFormat("int a = (int)[foo bar:baz];");
647   // verifyFormat("return (int)[foo bar:baz];");
648   // verifyFormat("(void)[foo bar:baz];");
649   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
650 
651   // Binary operators.
652   verifyFormat("[foo bar:baz], [foo bar:baz];");
653   verifyFormat("[foo bar:baz] = [foo bar:baz];");
654   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
655   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
656   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
657   verifyFormat("[foo bar:baz] += [foo bar:baz];");
658   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
659   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
660   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
661   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
662   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
663   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
664   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
665   verifyFormat("[foo bar:baz] || [foo bar:baz];");
666   verifyFormat("[foo bar:baz] && [foo bar:baz];");
667   verifyFormat("[foo bar:baz] | [foo bar:baz];");
668   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
669   verifyFormat("[foo bar:baz] & [foo bar:baz];");
670   verifyFormat("[foo bar:baz] == [foo bar:baz];");
671   verifyFormat("[foo bar:baz] != [foo bar:baz];");
672   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
673   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
674   verifyFormat("[foo bar:baz] > [foo bar:baz];");
675   verifyFormat("[foo bar:baz] < [foo bar:baz];");
676   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
677   verifyFormat("[foo bar:baz] << [foo bar:baz];");
678   verifyFormat("[foo bar:baz] - [foo bar:baz];");
679   verifyFormat("[foo bar:baz] + [foo bar:baz];");
680   verifyFormat("[foo bar:baz] * [foo bar:baz];");
681   verifyFormat("[foo bar:baz] / [foo bar:baz];");
682   verifyFormat("[foo bar:baz] % [foo bar:baz];");
683   // Whew!
684 
685   verifyFormat("return in[42];");
686   verifyFormat("for (auto v : in[1]) {\n}");
687   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
688   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
689   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
690   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
691   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
692   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
693                "}");
694   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
695   verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];");
696   verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];");
697   verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));");
698   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
699   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
700 
701   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
702   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
703   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
704   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
705   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
706   verifyFormat("[button setAction:@selector(zoomOut:)];");
707   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
708 
709   verifyFormat("arr[[self indexForFoo:a]];");
710   verifyFormat("throw [self errorFor:a];");
711   verifyFormat("@throw [self errorFor:a];");
712 
713   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
714   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
715   verifyFormat("4 > 4 ? (id)a : (id)baz;");
716 
717   unsigned PreviousColumnLimit = Style.ColumnLimit;
718   Style.ColumnLimit = 50;
719   // Instead of:
720   // bool a =
721   //     ([object a:42] == 0 || [object a:42
722   //                                    b:42] == 0);
723   verifyFormat("bool a = ([object a:42] == 0 ||\n"
724                "          [object a:42 b:42] == 0);");
725   Style.ColumnLimit = PreviousColumnLimit;
726   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
727                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
728 
729   // This tests that the formatter doesn't break after "backing" but before ":",
730   // which would be at 80 columns.
731   verifyFormat(
732       "void f() {\n"
733       "  if ((self = [super initWithContentRect:contentRect\n"
734       "                               styleMask:styleMask ?: otherMask\n"
735       "                                 backing:NSBackingStoreBuffered\n"
736       "                                   defer:YES]))");
737 
738   verifyFormat(
739       "[foo checkThatBreakingAfterColonWorksOk:\n"
740       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
741 
742   verifyFormat("[myObj short:arg1 // Force line break\n"
743                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
744                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
745                "                error:arg4];");
746   verifyFormat(
747       "void f() {\n"
748       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
749       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
750       "                                     pos.width(), pos.height())\n"
751       "                styleMask:NSBorderlessWindowMask\n"
752       "                  backing:NSBackingStoreBuffered\n"
753       "                    defer:NO]);\n"
754       "}");
755   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
756                "                             with:contentsNativeView];");
757 
758   verifyFormat(
759       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
760       "           owner:nillllll];");
761 
762   verifyFormat(
763       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
764       "        forType:kBookmarkButtonDragType];");
765 
766   verifyFormat("[defaultCenter addObserver:self\n"
767                "                  selector:@selector(willEnterFullscreen)\n"
768                "                      name:kWillEnterFullscreenNotification\n"
769                "                    object:nil];");
770   verifyFormat("[image_rep drawInRect:drawRect\n"
771                "             fromRect:NSZeroRect\n"
772                "            operation:NSCompositeCopy\n"
773                "             fraction:1.0\n"
774                "       respectFlipped:NO\n"
775                "                hints:nil];");
776   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
777                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
778   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
779                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
780   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
781                "    aaaaaaaaaaaaaaaaaaaaaa];");
782 
783   verifyFormat(
784       "scoped_nsobject<NSTextField> message(\n"
785       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
786       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
787   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
788                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
789                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
790                "          aaaa:bbb];");
791   verifyFormat("[self param:function( //\n"
792                "                parameter)]");
793   verifyFormat(
794       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
795       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
796       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
797 
798   // Variadic parameters.
799   verifyFormat(
800       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
801   verifyFormat(
802       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
803       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
804       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
805 
806   verifyFormat("[self // break\n"
807                "      a:a\n"
808                "    aaa:aaa];");
809 
810   // Formats pair-parameters.
811   verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
812   verifyFormat("[I drawRectOn:surface //\n"
813                "       ofSize:aa:bbb\n"
814                "     atOrigin:cc:dd];");
815 
816   // Inline block as a first argument.
817   verifyFormat("[object justBlock:^{\n"
818                "  a = 42;\n"
819                "}];");
820   verifyFormat("[object\n"
821                "    justBlock:^{\n"
822                "      a = 42;\n"
823                "    }\n"
824                "     notBlock:42\n"
825                "            a:42];");
826   verifyFormat("[object\n"
827                "    firstBlock:^{\n"
828                "      a = 42;\n"
829                "    }\n"
830                "    blockWithLongerName:^{\n"
831                "      a = 42;\n"
832                "    }];");
833   verifyFormat("[object\n"
834                "    blockWithLongerName:^{\n"
835                "      a = 42;\n"
836                "    }\n"
837                "    secondBlock:^{\n"
838                "      a = 42;\n"
839                "    }];");
840   verifyFormat("[object\n"
841                "    firstBlock:^{\n"
842                "      a = 42;\n"
843                "    }\n"
844                "    notBlock:42\n"
845                "    secondBlock:^{\n"
846                "      a = 42;\n"
847                "    }];");
848 
849   // Space between cast rparen and selector name component.
850   verifyFormat("[((Foo *)foo) bar];");
851   verifyFormat("[((Foo *)foo) bar:1 blech:2];");
852 
853   Style.ColumnLimit = 20;
854   verifyFormat("aaaaa = [a aa:aa\n"
855                "           aa:aa];");
856   verifyFormat("aaaaaa = [aa aa:aa\n"
857                "             aa:aa];");
858 
859   // Message receiver taking multiple lines.
860   // Non-corner case.
861   verifyFormat("[[object block:^{\n"
862                "  return 42;\n"
863                "}] a:42 b:42];");
864   // Arguments just fit into one line.
865   verifyFormat("[[object block:^{\n"
866                "  return 42;\n"
867                "}] aaaaaaa:42 b:42];");
868   // Arguments just over a column limit.
869   verifyFormat("[[object block:^{\n"
870                "  return 42;\n"
871                "}] aaaaaaa:42\n"
872                "        bb:42];");
873   // Arguments just fit into one line.
874   Style.ColumnLimit = 23;
875   verifyFormat("[[obj a:42\n"
876                "      b:42\n"
877                "      c:42\n"
878                "      d:42] e:42 f:42];");
879 
880   // Arguments do not fit into one line with a receiver.
881   Style.ColumnLimit = 20;
882   verifyFormat("[[obj a:42] a:42\n"
883                "            b:42];");
884   verifyFormat("[[obj a:42] a:42\n"
885                "            b:42\n"
886                "            c:42];");
887   verifyFormat("[[obj aaaaaa:42\n"
888                "           b:42]\n"
889                "    cc:42\n"
890                "     d:42];");
891 
892   // Avoid breaking receiver expression.
893   Style.ColumnLimit = 30;
894   verifyFormat("fooooooo =\n"
895                "    [[obj fooo] aaa:42\n"
896                "                aaa:42];");
897   verifyFormat("[[[obj foo] bar] aa:42\n"
898                "                 bb:42\n"
899                "                 cc:42];");
900 
901   // Avoid breaking between unary operators and ObjC method expressions.
902   Style.ColumnLimit = 45;
903   verifyFormat("if (a012345678901234567890123 &&\n"
904                "    ![foo bar]) {\n"
905                "}");
906   verifyFormat("if (a012345678901234567890123 &&\n"
907                "    +[foo bar]) {\n"
908                "}");
909   verifyFormat("if (a012345678901234567890123 &&\n"
910                "    -[foo bar]) {\n"
911                "}");
912 
913   Style.ColumnLimit = 70;
914   verifyFormat(
915       "void f() {\n"
916       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
917       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
918       "                                 pos.width(), pos.height())\n"
919       "                syeMask:NSBorderlessWindowMask\n"
920       "                  bking:NSBackingStoreBuffered\n"
921       "                    der:NO]);\n"
922       "}");
923 
924   Style.ColumnLimit = 60;
925   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
926                "        .aaaaaaaa];"); // FIXME: Indentation seems off.
927   // FIXME: This violates the column limit.
928   verifyFormat(
929       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
930       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
931       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
932 
933   Style = getChromiumStyle(FormatStyle::LK_ObjC);
934   Style.ColumnLimit = 80;
935   verifyFormat(
936       "void f() {\n"
937       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
938       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
939       "                                     pos.width(), pos.height())\n"
940       "                styleMask:NSBorderlessWindowMask\n"
941       "                  backing:NSBackingStoreBuffered\n"
942       "                    defer:NO]);\n"
943       "}");
944 
945   // Respect continuation indent and colon alignment (e.g. when object name is
946   // short, and first selector is the longest one)
947   Style = getLLVMStyle();
948   Style.Language = FormatStyle::LK_ObjC;
949   Style.ContinuationIndentWidth = 8;
950   verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
951                "                       withObject:nil\n"
952                "                    waitUntilDone:false];");
953   verifyFormat("[self performSelector:@selector(loadAccessories)\n"
954                "        withObjectOnMainThread:nil\n"
955                "                 waitUntilDone:false];");
956   verifyFormat(
957       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
958       "        performSelectorOnMainThread:@selector(loadAccessories)\n"
959       "                         withObject:nil\n"
960       "                      waitUntilDone:false];");
961   verifyFormat(
962       "[self // force wrapping\n"
963       "        performSelectorOnMainThread:@selector(loadAccessories)\n"
964       "                         withObject:nil\n"
965       "                      waitUntilDone:false];");
966 
967   // The appropriate indentation is used after a block statement.
968   Style.ContinuationIndentWidth = 4;
969   verifyFormat(
970       "void aaaaaaaaaaaaaaaaaaaaa(int c) {\n"
971       "  if (c) {\n"
972       "    f();\n"
973       "  }\n"
974       "  [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd\n"
975       "      eeeeeeeeeeeeeeeeeeeeeeeeeeeee:^(fffffffffffffff gggggggg) {\n"
976       "        f(SSSSS, c);\n"
977       "      }];\n"
978       "}");
979 }
980 
981 TEST_F(FormatTestObjC, ObjCAt) {
982   verifyFormat("@autoreleasepool");
983   verifyFormat("@catch");
984   verifyFormat("@class");
985   verifyFormat("@compatibility_alias");
986   verifyFormat("@defs");
987   verifyFormat("@dynamic");
988   verifyFormat("@encode");
989   verifyFormat("@end");
990   verifyFormat("@finally");
991   verifyFormat("@implementation");
992   verifyFormat("@import");
993   verifyFormat("@interface");
994   verifyFormat("@optional");
995   verifyFormat("@package");
996   verifyFormat("@private");
997   verifyFormat("@property");
998   verifyFormat("@protected");
999   verifyFormat("@protocol");
1000   verifyFormat("@public");
1001   verifyFormat("@required");
1002   verifyFormat("@selector");
1003   verifyFormat("@synchronized");
1004   verifyFormat("@synthesize");
1005   verifyFormat("@throw");
1006   verifyFormat("@try");
1007 
1008   EXPECT_EQ("@interface", format("@ interface"));
1009 
1010   // The precise formatting of this doesn't matter, nobody writes code like
1011   // this.
1012   verifyFormat("@ /*foo*/ interface");
1013 }
1014 
1015 TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) {
1016   verifyFormat("void DoStuffWithBlockType(int (^)(char));");
1017   verifyFormat("int (^foo)(char, float);");
1018   verifyFormat("int (^foo[10])(char, float);");
1019   verifyFormat("int (^foo[kNumEntries])(char, float);");
1020   verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
1021   verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
1022 
1023   verifyFormat("int *p = ^int *() { //\n"
1024                "  return nullptr;\n"
1025                "}();");
1026 
1027   verifyFormat("int * (^p)(void) = ^int *(void) { //\n"
1028                "  return nullptr;\n"
1029                "};");
1030 
1031   // WebKit forces function braces onto a newline, but blocks should not.
1032   verifyFormat("int* p = ^int*() { //\n"
1033                "    return nullptr;\n"
1034                "}();",
1035                getWebKitStyle());
1036 }
1037 
1038 TEST_F(FormatTestObjC, ObjCSnippets) {
1039   verifyFormat("@autoreleasepool {\n"
1040                "  foo();\n"
1041                "}");
1042   verifyFormat("@class Foo, Bar;");
1043   verifyFormat("@compatibility_alias AliasName ExistingClass;");
1044   verifyFormat("@dynamic textColor;");
1045   verifyFormat("char *buf1 = @encode(int *);");
1046   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1047   verifyFormat("char *buf1 = @encode(int **);");
1048   verifyFormat("Protocol *proto = @protocol(p1);");
1049   verifyFormat("SEL s = @selector(foo:);");
1050   verifyFormat("@synchronized(self) {\n"
1051                "  f();\n"
1052                "}");
1053 
1054   verifyFormat("@import foo.bar;\n"
1055                "@import baz;");
1056 
1057   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1058 
1059   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
1060   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1061 
1062   verifyFormat("extern UIWindow *MainWindow(void) "
1063                "NS_SWIFT_NAME(getter:MyHelper.mainWindow());");
1064 
1065   verifyFormat("extern UIWindow *MainWindow(void) "
1066                "CF_SWIFT_NAME(getter:MyHelper.mainWindow());");
1067 
1068   Style.ColumnLimit = 50;
1069   verifyFormat("@interface Foo\n"
1070                "- (void)doStuffWithFoo:(id)name\n"
1071                "                   bar:(id)bar\n"
1072                "                   baz:(id)baz\n"
1073                "    NS_SWIFT_NAME(doStuff(withFoo:bar:baz:));\n"
1074                "@end");
1075 
1076   Style = getMozillaStyle();
1077   verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
1078   verifyFormat("@property BOOL editable;");
1079 
1080   Style = getWebKitStyle();
1081   verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
1082   verifyFormat("@property BOOL editable;");
1083 
1084   Style = getGoogleStyle(FormatStyle::LK_ObjC);
1085   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1086   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1087 }
1088 
1089 TEST_F(FormatTestObjC, ObjCForIn) {
1090   verifyFormat("- (void)test {\n"
1091                "  for (NSString *n in arrayOfStrings) {\n"
1092                "    foo(n);\n"
1093                "  }\n"
1094                "}");
1095   verifyFormat("- (void)test {\n"
1096                "  for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
1097                "    foo(n);\n"
1098                "  }\n"
1099                "}");
1100   verifyFormat("for (Foo *x in bar) {\n}");
1101   verifyFormat("for (Foo *x in [bar baz]) {\n}");
1102   verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
1103   verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
1104   verifyFormat("for (Foo *x in [bar baz:^{\n"
1105                "       [uh oh];\n"
1106                "     }]) {\n}");
1107 }
1108 
1109 TEST_F(FormatTestObjC, ObjCCxxKeywords) {
1110   verifyFormat("+ (instancetype)new {\n"
1111                "  return nil;\n"
1112                "}\n");
1113   verifyFormat("+ (instancetype)myNew {\n"
1114                "  return [self new];\n"
1115                "}\n");
1116   verifyFormat("SEL NewSelector(void) { return @selector(new); }\n");
1117   verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n");
1118   verifyFormat("+ (instancetype)delete {\n"
1119                "  return nil;\n"
1120                "}\n");
1121   verifyFormat("+ (instancetype)myDelete {\n"
1122                "  return [self delete];\n"
1123                "}\n");
1124   verifyFormat("SEL DeleteSelector(void) { return @selector(delete); }\n");
1125   verifyFormat("SEL MacroSelector(void) { return MACRO(delete); }\n");
1126   verifyFormat("MACRO(new:)\n");
1127   verifyFormat("MACRO(delete:)\n");
1128   verifyFormat("foo = @{MACRO(new:) : MACRO(delete:)}\n");
1129   verifyFormat("@implementation Foo\n"
1130                "// Testing\n"
1131                "- (Class)class {\n"
1132                "}\n"
1133                "- (void)foo {\n"
1134                "}\n"
1135                "@end\n");
1136   verifyFormat("@implementation Foo\n"
1137                "- (Class)class {\n"
1138                "}\n"
1139                "- (void)foo {\n"
1140                "}\n"
1141                "@end");
1142   verifyFormat("@implementation Foo\n"
1143                "+ (Class)class {\n"
1144                "}\n"
1145                "- (void)foo {\n"
1146                "}\n"
1147                "@end");
1148   verifyFormat("@implementation Foo\n"
1149                "- (Class)class:(Class)klass {\n"
1150                "}\n"
1151                "- (void)foo {\n"
1152                "}\n"
1153                "@end");
1154   verifyFormat("@implementation Foo\n"
1155                "+ (Class)class:(Class)klass {\n"
1156                "}\n"
1157                "- (void)foo {\n"
1158                "}\n"
1159                "@end");
1160 
1161   verifyFormat("@interface Foo\n"
1162                "// Testing\n"
1163                "- (Class)class;\n"
1164                "- (void)foo;\n"
1165                "@end\n");
1166   verifyFormat("@interface Foo\n"
1167                "- (Class)class;\n"
1168                "- (void)foo;\n"
1169                "@end");
1170   verifyFormat("@interface Foo\n"
1171                "+ (Class)class;\n"
1172                "- (void)foo;\n"
1173                "@end");
1174   verifyFormat("@interface Foo\n"
1175                "- (Class)class:(Class)klass;\n"
1176                "- (void)foo;\n"
1177                "@end");
1178   verifyFormat("@interface Foo\n"
1179                "+ (Class)class:(Class)klass;\n"
1180                "- (void)foo;\n"
1181                "@end");
1182 }
1183 
1184 TEST_F(FormatTestObjC, ObjCLiterals) {
1185   verifyFormat("@\"String\"");
1186   verifyFormat("@1");
1187   verifyFormat("@+4.8");
1188   verifyFormat("@-4");
1189   verifyFormat("@1LL");
1190   verifyFormat("@.5");
1191   verifyFormat("@'c'");
1192   verifyFormat("@true");
1193 
1194   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
1195   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
1196   verifyFormat("NSNumber *favoriteColor = @(Green);");
1197   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
1198 
1199   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
1200 }
1201 
1202 TEST_F(FormatTestObjC, ObjCDictLiterals) {
1203   verifyFormat("@{");
1204   verifyFormat("@{}");
1205   verifyFormat("@{@\"one\" : @1}");
1206   verifyFormat("return @{@\"one\" : @1;");
1207   verifyFormat("@{@\"one\" : @1}");
1208 
1209   verifyFormat("@{@\"one\" : @{@2 : @1}}");
1210   verifyFormat("@{\n"
1211                "  @\"one\" : @{@2 : @1},\n"
1212                "}");
1213 
1214   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
1215   verifyIncompleteFormat("[self setDict:@{}");
1216   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
1217   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
1218   verifyFormat(
1219       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
1220   verifyFormat(
1221       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
1222 
1223   verifyFormat("NSDictionary *d = @{\n"
1224                "  @\"nam\" : NSUserNam(),\n"
1225                "  @\"dte\" : [NSDate date],\n"
1226                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
1227                "};");
1228   verifyFormat(
1229       "@{\n"
1230       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1231       "regularFont,\n"
1232       "};");
1233   verifyFormat(
1234       "@{\n"
1235       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
1236       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
1237       "};");
1238 
1239   // We should try to be robust in case someone forgets the "@".
1240   verifyFormat("NSDictionary *d = {\n"
1241                "  @\"nam\" : NSUserNam(),\n"
1242                "  @\"dte\" : [NSDate date],\n"
1243                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
1244                "};");
1245   verifyFormat("NSMutableDictionary *dictionary =\n"
1246                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
1247                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
1248                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
1249                "      cccccccccccccccc : ccccccccccccccc\n"
1250                "    }];");
1251 
1252   // Ensure that casts before the key are kept on the same line as the key.
1253   verifyFormat(
1254       "NSDictionary *d = @{\n"
1255       "  (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1256       "  (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
1257       "};");
1258   Style.ColumnLimit = 40;
1259   verifyFormat("int Foo() {\n"
1260                "  a12345 = @{a12345 : a12345};\n"
1261                "}");
1262   verifyFormat("int Foo() {\n"
1263                "  a12345 = @{a12345 : @(a12345)};\n"
1264                "}");
1265   verifyFormat("int Foo() {\n"
1266                "  a12345 = @{(Foo *)a12345 : @(a12345)};\n"
1267                "}");
1268   verifyFormat("int Foo() {\n"
1269                "  a12345 = @{@(a12345) : a12345};\n"
1270                "}");
1271   verifyFormat("int Foo() {\n"
1272                "  a12345 = @{@(a12345) : @YES};\n"
1273                "}");
1274   Style.SpacesInContainerLiterals = false;
1275   verifyFormat("int Foo() {\n"
1276                "  b12345 = @{b12345: b12345};\n"
1277                "}");
1278   verifyFormat("int Foo() {\n"
1279                "  b12345 = @{(Foo *)b12345: @(b12345)};\n"
1280                "}");
1281   Style.SpacesInContainerLiterals = true;
1282 
1283   Style = getGoogleStyle(FormatStyle::LK_ObjC);
1284   verifyFormat(
1285       "@{\n"
1286       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1287       "regularFont,\n"
1288       "};");
1289 }
1290 
1291 TEST_F(FormatTestObjC, ObjCArrayLiterals) {
1292   verifyIncompleteFormat("@[");
1293   verifyFormat("@[]");
1294   verifyFormat(
1295       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
1296   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
1297   verifyFormat("NSArray *array = @[ [foo description] ];");
1298 
1299   verifyFormat(
1300       "NSArray *some_variable = @[\n"
1301       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1302       "  @\"aaaaaaaaaaaaaaaaa\",\n"
1303       "  @\"aaaaaaaaaaaaaaaaa\",\n"
1304       "  @\"aaaaaaaaaaaaaaaaa\",\n"
1305       "];");
1306   verifyFormat(
1307       "NSArray *some_variable = @[\n"
1308       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1309       "  @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1310       "];");
1311   verifyFormat("NSArray *some_variable = @[\n"
1312                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1313                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1314                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1315                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1316                "];");
1317   verifyFormat("NSArray *array = @[\n"
1318                "  @\"a\",\n"
1319                "  @\"a\",\n" // Trailing comma -> one per line.
1320                "];");
1321 
1322   // We should try to be robust in case someone forgets the "@".
1323   verifyFormat("NSArray *some_variable = [\n"
1324                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1325                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1326                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1327                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1328                "];");
1329   verifyFormat(
1330       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1331       "                                             index:(NSUInteger)index\n"
1332       "                                nonDigitAttributes:\n"
1333       "                                    (NSDictionary *)noDigitAttributes;");
1334   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1335                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1336                "]];");
1337   Style.ColumnLimit = 40;
1338   verifyFormat("int Foo() {\n"
1339                "  a12345 = @[ a12345, a12345 ];\n"
1340                "}");
1341   verifyFormat("int Foo() {\n"
1342                "  a123 = @[ (Foo *)a12345, @(a12345) ];\n"
1343                "}");
1344   Style.SpacesInContainerLiterals = false;
1345   verifyFormat("int Foo() {\n"
1346                "  b12345 = @[b12345, b12345];\n"
1347                "}");
1348   verifyFormat("int Foo() {\n"
1349                "  b12345 = @[(Foo *)b12345, @(b12345)];\n"
1350                "}");
1351   Style.SpacesInContainerLiterals = true;
1352   Style.ColumnLimit = 20;
1353   // We can't break string literals inside NSArray literals
1354   // (that raises -Wobjc-string-concatenation).
1355   verifyFormat("NSArray *foo = @[\n"
1356                "  @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1357                "];\n");
1358 }
1359 
1360 TEST_F(FormatTestObjC, BreaksCallStatementWhereSemiJustOverTheLimit) {
1361   Style.ColumnLimit = 60;
1362   // If the statement starting with 'a = ...' is put on a single line, the ';'
1363   // is at line 61.
1364   verifyFormat("int f(int a) {\n"
1365                "  a = [self aaaaaaaaaa:bbbbbbbbb\n"
1366                "             ccccccccc:dddddddd\n"
1367                "                    ee:fddd];\n"
1368                "}");
1369 }
1370 
1371 TEST_F(FormatTestObjC, AlwaysBreakBeforeMultilineStrings) {
1372   Style = getGoogleStyle(FormatStyle::LK_ObjC);
1373   Style.ColumnLimit = 40;
1374   verifyFormat("aaaa = @\"bbbb\"\n"
1375                "       @\"cccc\";");
1376   verifyFormat("aaaa(@\"bbbb\"\n"
1377                "     @\"cccc\");");
1378   verifyFormat("aaaa(qqq, @\"bbbb\"\n"
1379                "          @\"cccc\");");
1380   verifyFormat("[aaaa qqqq:@\"bbbb\"\n"
1381                "           @\"cccc\"];");
1382   verifyFormat("aaaa = [aaaa qqqq:@\"bbbb\"\n"
1383                "                  @\"cccc\"];");
1384   verifyFormat("[aaaa qqqq:@\"bbbb\"\n"
1385                "           @\"cccc\"\n"
1386                "        rr:42\n"
1387                "    ssssss:@\"ee\"\n"
1388                "           @\"fffff\"];");
1389 }
1390 
1391 TEST_F(FormatTestObjC, DisambiguatesCallsFromCppLambdas) {
1392   verifyFormat("x = ([a foo:bar] && b->c == 'd');");
1393   verifyFormat("x = ([a foo:bar] + b->c == 'd');");
1394   verifyFormat("x = ([a foo:bar] + !b->c == 'd');");
1395   verifyFormat("x = ([a foo:bar] + ~b->c == 'd');");
1396   verifyFormat("x = ([a foo:bar] - b->c == 'd');");
1397   verifyFormat("x = ([a foo:bar] / b->c == 'd');");
1398   verifyFormat("x = ([a foo:bar] % b->c == 'd');");
1399   verifyFormat("x = ([a foo:bar] | b->c == 'd');");
1400   verifyFormat("x = ([a foo:bar] || b->c == 'd');");
1401   verifyFormat("x = ([a foo:bar] && b->c == 'd');");
1402   verifyFormat("x = ([a foo:bar] == b->c == 'd');");
1403   verifyFormat("x = ([a foo:bar] != b->c == 'd');");
1404   verifyFormat("x = ([a foo:bar] <= b->c == 'd');");
1405   verifyFormat("x = ([a foo:bar] >= b->c == 'd');");
1406   verifyFormat("x = ([a foo:bar] << b->c == 'd');");
1407   verifyFormat("x = ([a foo:bar] ? b->c == 'd' : 'e');");
1408   // FIXME: The following are wrongly classified as C++ lambda expressions.
1409   // For example this code:
1410   //   x = ([a foo:bar] & b->c == 'd');
1411   // is formatted as:
1412   //   x = ([a foo:bar] & b -> c == 'd');
1413   // verifyFormat("x = ([a foo:bar] & b->c == 'd');");
1414   // verifyFormat("x = ([a foo:bar] > b->c == 'd');");
1415   // verifyFormat("x = ([a foo:bar] < b->c == 'd');");
1416   // verifyFormat("x = ([a foo:bar] >> b->c == 'd');");
1417 }
1418 
1419 TEST_F(FormatTestObjC, DisambiguatesCallsFromStructuredBindings) {
1420   verifyFormat("int f() {\n"
1421                "  if (a && [f arg])\n"
1422                "    return 0;\n"
1423                "}");
1424   verifyFormat("int f() {\n"
1425                "  if (a & [f arg])\n"
1426                "    return 0;\n"
1427                "}");
1428   verifyFormat("int f() {\n"
1429                "  for (auto &[elem] : list)\n"
1430                "    return 0;\n"
1431                "}");
1432   verifyFormat("int f() {\n"
1433                "  for (auto &&[elem] : list)\n"
1434                "    return 0;\n"
1435                "}");
1436   verifyFormat(
1437       "int f() {\n"
1438       "  for (auto /**/ const /**/ volatile /**/ && /**/ [elem] : list)\n"
1439       "    return 0;\n"
1440       "}");
1441 }
1442 
1443 TEST_F(FormatTestObjC, BreakLineBeforeNestedBlockParam) {
1444   Style = getGoogleStyle(FormatStyle::LK_ObjC);
1445   Style.ObjCBreakBeforeNestedBlockParam = false;
1446   Style.ColumnLimit = 0;
1447 
1448   verifyFormat("[self.test1 t:self callback:^(typeof(self) self, NSNumber *u, "
1449                "NSNumber *v) {\n"
1450                "  u = v;\n"
1451                "}]");
1452 
1453   verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1454                "NSNumber *u, NSNumber *v) {\n"
1455                "  u = v;\n"
1456                "}]");
1457 
1458   verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1459                "NSNumber *u, NSNumber *v) {\n"
1460                "  u = c;\n"
1461                "} w:self callback2:^(typeof(self) self, NSNumber *a, NSNumber "
1462                "*b, NSNumber *c) {\n"
1463                "  b = c;\n"
1464                "}]");
1465   verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1466                "NSNumber *u, NSNumber *v) {\n"
1467                "  u = v;\n"
1468                "} z:self]");
1469 
1470   Style.ColumnLimit = 80;
1471   verifyFormat(
1472       "[self.test_method a:self b:self\n"
1473       "           callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {\n"
1474       "             u = v;\n"
1475       "           }]");
1476 
1477   verifyFormat("[self block:^(void) {\n"
1478                "  doStuff();\n"
1479                "} completionHandler:^(void) {\n"
1480                "  doStuff();\n"
1481                "  [self block:^(void) {\n"
1482                "    doStuff();\n"
1483                "  } completionHandler:^(void) {\n"
1484                "    doStuff();\n"
1485                "  }];\n"
1486                "}];");
1487 
1488   Style.ColumnLimit = 0;
1489   verifyFormat("[[SessionService sharedService] "
1490                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
1491                "  if (window) {\n"
1492                "    [self windowDidLoad:window];\n"
1493                "  } else {\n"
1494                "    [self errorLoadingWindow];\n"
1495                "  }\n"
1496                "}];");
1497   verifyFormat("[controller test:^{\n"
1498                "  doStuff();\n"
1499                "} withTimeout:5 completionHandler:^{\n"
1500                "  doStuff();\n"
1501                "}];");
1502   verifyFormat(
1503       "[self setupTextFieldSignals:@[\n"
1504       "  self.documentWidthField,\n"
1505       "  self.documentHeightField,\n"
1506       "] solver:^(NSTextField *textField) {\n"
1507       "  return [self.representedObject solveEquationForTextField:textField];\n"
1508       "}];");
1509 }
1510 
1511 TEST_F(FormatTestObjC, IfNotUnlikely) {
1512   Style = getGoogleStyle(FormatStyle::LK_ObjC);
1513 
1514   verifyFormat("if (argc < 5) [obj func:arg];");
1515   verifyFormat("if (argc < 5) [[obj1 method1:arg1] method2:arg2];");
1516   verifyFormat("if (argc < 5) [[foo bar] baz:i[0]];");
1517   verifyFormat("if (argc < 5) [[foo bar] baz:i[0]][1];");
1518 
1519   verifyFormat("if (argc < 5)\n"
1520                "  [obj func:arg];\n"
1521                "else\n"
1522                "  [obj func:arg2];");
1523 
1524   verifyFormat("if (argc < 5) [[unlikely]]\n"
1525                "  [obj func:arg];\n"
1526                "else [[likely]]\n"
1527                "  [obj func:arg2];");
1528 }
1529 
1530 TEST_F(FormatTestObjC, Attributes) {
1531   verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1532                "@interface Foo\n"
1533                "@end");
1534   verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1535                "@protocol Foo\n"
1536                "@end");
1537   verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1538                "@implementation Foo\n"
1539                "@end");
1540 }
1541 
1542 } // end namespace
1543 } // namespace test
1544 } // end namespace format
1545 } // end namespace clang
1546