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