8773088c82e5f68e8f0a8f2d766b667d98d06c41
[lldb.git] / clang / unittests / Format / FormatTestObjC.cpp
1 //===- unittest/Format/FormatTestObjC.cpp - Formatting unit tests----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "clang/Format/Format.h"
11
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
14
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19
20 #define DEBUG_TYPE "format-test"
21
22 using clang::tooling::ReplacementTest;
23
24 namespace clang {
25 namespace format {
26 namespace {
27
28 class FormatTestObjC : public ::testing::Test {
29 protected:
30   FormatTestObjC() {
31     Style = getLLVMStyle();
32     Style.Language = FormatStyle::LK_ObjC;
33   }
34
35   enum StatusCheck {
36     SC_ExpectComplete,
37     SC_ExpectIncomplete,
38     SC_DoNotCheck
39   };
40
41   std::string format(llvm::StringRef Code,
42                      StatusCheck CheckComplete = SC_ExpectComplete) {
43     DEBUG(llvm::errs() << "---\n");
44     DEBUG(llvm::errs() << Code << "\n\n");
45     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
46     FormattingAttemptStatus Status;
47     tooling::Replacements Replaces =
48         reformat(Style, Code, Ranges, "<stdin>", &Status);
49     if (CheckComplete != SC_DoNotCheck) {
50       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
51       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
52           << Code << "\n\n";
53     }
54     auto Result = applyAllReplacements(Code, Replaces);
55     EXPECT_TRUE(static_cast<bool>(Result));
56     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
57     return *Result;
58   }
59
60   void verifyFormat(StringRef Code) {
61     EXPECT_EQ(Code.str(), format(test::messUp(Code)));
62   }
63
64   void verifyIncompleteFormat(StringRef Code) {
65     EXPECT_EQ(Code.str(), format(test::messUp(Code), SC_ExpectIncomplete));
66   }
67
68   FormatStyle Style;
69 };
70
71 TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
72   auto Style = getStyle("LLVM", "a.h", "none", "@interface\n"
73                                                "- (id)init;");
74   ASSERT_TRUE((bool)Style);
75   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
76
77   Style = getStyle("LLVM", "a.h", "none", "@interface\n"
78                                           "+ (id)init;");
79   ASSERT_TRUE((bool)Style);
80   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
81
82   Style = getStyle("LLVM", "a.h", "none", "@interface\n"
83                                           "@end\n"
84                                           "//comment");
85   ASSERT_TRUE((bool)Style);
86   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
87
88   Style = getStyle("LLVM", "a.h", "none", "@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 =
117       getStyle("{}", "a.h", "none", "typedef NS_ENUM(NSInteger, Foo) {};\n");
118   ASSERT_TRUE((bool)Style);
119   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
120
121   Style = getStyle("{}", "a.h", "none", "enum Foo {};");
122   ASSERT_TRUE((bool)Style);
123   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
124
125   Style =
126       getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n");
127   ASSERT_TRUE((bool)Style);
128   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
129
130   Style =
131       getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }\n");
132   ASSERT_TRUE((bool)Style);
133   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
134
135   Style =
136       getStyle("{}", "a.h", "none", "inline void Foo() { id = @[1, 2, 3]; }\n");
137   ASSERT_TRUE((bool)Style);
138   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
139
140   Style = getStyle("{}", "a.h", "none",
141                    "inline void Foo() { id foo = @{1: 2, 3: 4, 5: 6}; }\n");
142   ASSERT_TRUE((bool)Style);
143   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
144
145   Style = getStyle("{}", "a.h", "none",
146                    "inline void Foo() { int foo[] = {1, 2, 3}; }\n");
147   ASSERT_TRUE((bool)Style);
148   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
149
150   // ObjC characteristic types.
151   Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
152   ASSERT_TRUE((bool)Style);
153   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
154
155   Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
156   ASSERT_TRUE((bool)Style);
157   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
158
159   Style = getStyle("{}", "a.h", "none", "NSObject *Foo();\n");
160   ASSERT_TRUE((bool)Style);
161   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
162
163   Style = getStyle("{}", "a.h", "none", "NSSet *Foo();\n");
164   ASSERT_TRUE((bool)Style);
165   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
166 }
167
168 TEST_F(FormatTestObjC, FormatObjCTryCatch) {
169   verifyFormat("@try {\n"
170                "  f();\n"
171                "} @catch (NSException e) {\n"
172                "  @throw;\n"
173                "} @finally {\n"
174                "  exit(42);\n"
175                "}");
176   verifyFormat("DEBUG({\n"
177                "  @try {\n"
178                "  } @finally {\n"
179                "  }\n"
180                "});\n");
181 }
182
183 TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
184   verifyFormat("@autoreleasepool {\n"
185                "  f();\n"
186                "}\n"
187                "@autoreleasepool {\n"
188                "  f();\n"
189                "}\n");
190   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
191   Style.BraceWrapping.AfterControlStatement = true;
192   verifyFormat("@autoreleasepool\n"
193                "{\n"
194                "  f();\n"
195                "}\n"
196                "@autoreleasepool\n"
197                "{\n"
198                "  f();\n"
199                "}\n");
200 }
201
202 TEST_F(FormatTestObjC, FormatObjCGenerics) {
203   Style.ColumnLimit = 40;
204   verifyFormat("int aaaaaaaaaaaaaaaa(\n"
205                "    NSArray<aaaaaaaaaaaaaaaaaa *>\n"
206                "        aaaaaaaaaaaaaaaaa);\n");
207   verifyFormat("int aaaaaaaaaaaaaaaa(\n"
208                "    NSArray<aaaaaaaaaaaaaaaaaaa<\n"
209                "        aaaaaaaaaaaaaaaa *> *>\n"
210                "        aaaaaaaaaaaaaaaaa);\n");
211 }
212
213 TEST_F(FormatTestObjC, FormatObjCSynchronized) {
214   verifyFormat("@synchronized(self) {\n"
215                "  f();\n"
216                "}\n"
217                "@synchronized(self) {\n"
218                "  f();\n"
219                "}\n");
220   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
221   Style.BraceWrapping.AfterControlStatement = true;
222   verifyFormat("@synchronized(self)\n"
223                "{\n"
224                "  f();\n"
225                "}\n"
226                "@synchronized(self)\n"
227                "{\n"
228                "  f();\n"
229                "}\n");
230 }
231
232 TEST_F(FormatTestObjC, FormatObjCInterface) {
233   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
234                "@public\n"
235                "  int field1;\n"
236                "@protected\n"
237                "  int field2;\n"
238                "@private\n"
239                "  int field3;\n"
240                "@package\n"
241                "  int field4;\n"
242                "}\n"
243                "+ (id)init;\n"
244                "@end");
245
246   verifyFormat("@interface /* wait for it */ Foo\n"
247                "+ (id)init;\n"
248                "// Look, a comment!\n"
249                "- (int)answerWith:(int)i;\n"
250                "@end");
251
252   verifyFormat("@interface Foo\n"
253                "@end\n"
254                "@interface Bar\n"
255                "@end");
256
257   verifyFormat("@interface Foo : Bar\n"
258                "@property(assign, readwrite) NSInteger bar;\n"
259                "+ (id)init;\n"
260                "@end");
261
262   verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n"
263                "@property(assign, readwrite) NSInteger bar;\n"
264                "+ (id)init;\n"
265                "@end");
266
267   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
268                "+ (id)init;\n"
269                "@end");
270
271   verifyFormat("@interface Foo (HackStuff)\n"
272                "+ (id)init;\n"
273                "@end");
274
275   verifyFormat("@interface Foo ()\n"
276                "+ (id)init;\n"
277                "@end");
278
279   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
280                "+ (id)init;\n"
281                "@end");
282
283   verifyFormat("@interface Foo {\n"
284                "  int _i;\n"
285                "}\n"
286                "+ (id)init;\n"
287                "@end");
288
289   verifyFormat("@interface Foo : Bar {\n"
290                "  int _i;\n"
291                "}\n"
292                "+ (id)init;\n"
293                "@end");
294
295   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
296                "  int _i;\n"
297                "}\n"
298                "+ (id)init;\n"
299                "@end");
300
301   verifyFormat("@interface Foo (HackStuff) {\n"
302                "  int _i;\n"
303                "}\n"
304                "+ (id)init;\n"
305                "@end");
306
307   verifyFormat("@interface Foo () {\n"
308                "  int _i;\n"
309                "}\n"
310                "+ (id)init;\n"
311                "@end");
312
313   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
314                "  int _i;\n"
315                "}\n"
316                "+ (id)init;\n"
317                "@end");
318
319   Style.ColumnLimit = 40;
320   verifyFormat("@interface ccccccccccccc () <\n"
321                "    ccccccccccccc, ccccccccccccc,\n"
322                "    ccccccccccccc, ccccccccccccc> {\n"
323                "}");
324   Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
325   verifyFormat("@interface ddddddddddddd () <\n"
326                "    ddddddddddddd,\n"
327                "    ddddddddddddd,\n"
328                "    ddddddddddddd,\n"
329                "    ddddddddddddd> {\n"
330                "}");
331
332   Style.BinPackParameters = false;
333   Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
334   verifyFormat("@interface eeeeeeeeeeeee () <\n"
335                "    eeeeeeeeeeeee,\n"
336                "    eeeeeeeeeeeee,\n"
337                "    eeeeeeeeeeeee,\n"
338                "    eeeeeeeeeeeee> {\n"
339                "}");
340   Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always;
341   verifyFormat("@interface fffffffffffff () <\n"
342                "    fffffffffffff, fffffffffffff,\n"
343                "    fffffffffffff, fffffffffffff> {\n"
344                "}");
345
346   Style = getGoogleStyle(FormatStyle::LK_ObjC);
347   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
348                " @public\n"
349                "  int field1;\n"
350                " @protected\n"
351                "  int field2;\n"
352                " @private\n"
353                "  int field3;\n"
354                " @package\n"
355                "  int field4;\n"
356                "}\n"
357                "+ (id)init;\n"
358                "@end");
359   verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
360                "+ (id)init;\n"
361                "@end");
362   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
363                "+ (id)init;\n"
364                "@end");
365   Style.ColumnLimit = 40;
366   // BinPackParameters should be true by default.
367   verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
368                "              int eeeee, int eeeee);\n");
369   // ObjCBinPackProtocolList should be BPS_Never by default.
370   verifyFormat("@interface fffffffffffff () <\n"
371                "    fffffffffffff,\n"
372                "    fffffffffffff,\n"
373                "    fffffffffffff,\n"
374                "    fffffffffffff> {\n"
375                "}");
376 }
377
378 TEST_F(FormatTestObjC, FormatObjCImplementation) {
379   verifyFormat("@implementation Foo : NSObject {\n"
380                "@public\n"
381                "  int field1;\n"
382                "@protected\n"
383                "  int field2;\n"
384                "@private\n"
385                "  int field3;\n"
386                "@package\n"
387                "  int field4;\n"
388                "}\n"
389                "+ (id)init {\n}\n"
390                "@end");
391
392   verifyFormat("@implementation Foo\n"
393                "+ (id)init {\n"
394                "  if (true)\n"
395                "    return nil;\n"
396                "}\n"
397                "// Look, a comment!\n"
398                "- (int)answerWith:(int)i {\n"
399                "  return i;\n"
400                "}\n"
401                "+ (int)answerWith:(int)i {\n"
402                "  return i;\n"
403                "}\n"
404                "@end");
405
406   verifyFormat("@implementation Foo\n"
407                "@end\n"
408                "@implementation Bar\n"
409                "@end");
410
411   EXPECT_EQ("@implementation Foo : Bar\n"
412             "+ (id)init {\n}\n"
413             "- (void)foo {\n}\n"
414             "@end",
415             format("@implementation Foo : Bar\n"
416                    "+(id)init{}\n"
417                    "-(void)foo{}\n"
418                    "@end"));
419
420   verifyFormat("@implementation Foo {\n"
421                "  int _i;\n"
422                "}\n"
423                "+ (id)init {\n}\n"
424                "@end");
425
426   verifyFormat("@implementation Foo : Bar {\n"
427                "  int _i;\n"
428                "}\n"
429                "+ (id)init {\n}\n"
430                "@end");
431
432   verifyFormat("@implementation Foo (HackStuff)\n"
433                "+ (id)init {\n}\n"
434                "@end");
435   verifyFormat("@implementation ObjcClass\n"
436                "- (void)method;\n"
437                "{}\n"
438                "@end");
439
440   Style = getGoogleStyle(FormatStyle::LK_ObjC);
441   verifyFormat("@implementation Foo : NSObject {\n"
442                " @public\n"
443                "  int field1;\n"
444                " @protected\n"
445                "  int field2;\n"
446                " @private\n"
447                "  int field3;\n"
448                " @package\n"
449                "  int field4;\n"
450                "}\n"
451                "+ (id)init {\n}\n"
452                "@end");
453 }
454
455 TEST_F(FormatTestObjC, FormatObjCProtocol) {
456   verifyFormat("@protocol Foo\n"
457                "@property(weak) id delegate;\n"
458                "- (NSUInteger)numberOfThings;\n"
459                "@end");
460
461   verifyFormat("@protocol MyProtocol <NSObject>\n"
462                "- (NSUInteger)numberOfThings;\n"
463                "@end");
464
465   verifyFormat("@protocol Foo;\n"
466                "@protocol Bar;\n");
467
468   verifyFormat("@protocol Foo\n"
469                "@end\n"
470                "@protocol Bar\n"
471                "@end");
472
473   verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
474                "@property(assign, readwrite) NSInteger bar;\n"
475                "@end");
476
477   verifyFormat("@protocol myProtocol\n"
478                "- (void)mandatoryWithInt:(int)i;\n"
479                "@optional\n"
480                "- (void)optional;\n"
481                "@required\n"
482                "- (void)required;\n"
483                "@optional\n"
484                "@property(assign) int madProp;\n"
485                "@end\n");
486
487   verifyFormat("@property(nonatomic, assign, readonly)\n"
488                "    int *looooooooooooooooooooooooooooongNumber;\n"
489                "@property(nonatomic, assign, readonly)\n"
490                "    NSString *looooooooooooooooooooooooooooongName;");
491
492   verifyFormat("@implementation PR18406\n"
493                "}\n"
494                "@end");
495
496   Style = getGoogleStyle(FormatStyle::LK_ObjC);
497   verifyFormat("@protocol MyProtocol <NSObject>\n"
498                "- (NSUInteger)numberOfThings;\n"
499                "@end");
500 }
501
502 TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
503   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
504                "                   rect:(NSRect)theRect\n"
505                "               interval:(float)theInterval {\n"
506                "}");
507   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
508                "      longKeyword:(NSRect)theRect\n"
509                "    longerKeyword:(float)theInterval\n"
510                "            error:(NSError **)theError {\n"
511                "}");
512   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
513                "          longKeyword:(NSRect)theRect\n"
514                "    evenLongerKeyword:(float)theInterval\n"
515                "                error:(NSError **)theError {\n"
516                "}");
517   Style.ColumnLimit = 60;
518   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
519                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
520                "    NS_DESIGNATED_INITIALIZER;");
521   verifyFormat("- (void)drawRectOn:(id)surface\n"
522                "            ofSize:(size_t)height\n"
523                "                  :(size_t)width;");
524
525   // Continuation indent width should win over aligning colons if the function
526   // name is long.
527   Style = getGoogleStyle(FormatStyle::LK_ObjC);
528   Style.ColumnLimit = 40;
529   Style.IndentWrappedFunctionNames = true;
530   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
531                "    dontAlignNamef:(NSRect)theRect {\n"
532                "}");
533
534   // Make sure we don't break aligning for short parameter names.
535   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
536                "       aShortf:(NSRect)theRect {\n"
537                "}");
538
539   // Format pairs correctly.
540   Style.ColumnLimit = 80;
541   verifyFormat("- (void)drawRectOn:(id)surface\n"
542                "            ofSize:(aaaaaaaa)height\n"
543                "                  :(size_t)width\n"
544                "          atOrigin:(size_t)x\n"
545                "                  :(size_t)y\n"
546                "             aaaaa:(a)yyy\n"
547                "               bbb:(d)cccc;");
548   verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
549 }
550
551 TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
552   verifyFormat("[foo bar:baz];");
553   verifyFormat("return [foo bar:baz];");
554   verifyFormat("return (a)[foo bar:baz];");
555   verifyFormat("f([foo bar:baz]);");
556   verifyFormat("f(2, [foo bar:baz]);");
557   verifyFormat("f(2, a ? b : c);");
558   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
559
560   // Unary operators.
561   verifyFormat("int a = +[foo bar:baz];");
562   verifyFormat("int a = -[foo bar:baz];");
563   verifyFormat("int a = ![foo bar:baz];");
564   verifyFormat("int a = ~[foo bar:baz];");
565   verifyFormat("int a = ++[foo bar:baz];");
566   verifyFormat("int a = --[foo bar:baz];");
567   verifyFormat("int a = sizeof [foo bar:baz];");
568   verifyFormat("int a = alignof [foo bar:baz];");
569   verifyFormat("int a = &[foo bar:baz];");
570   verifyFormat("int a = *[foo bar:baz];");
571   // FIXME: Make casts work, without breaking f()[4].
572   // verifyFormat("int a = (int)[foo bar:baz];");
573   // verifyFormat("return (int)[foo bar:baz];");
574   // verifyFormat("(void)[foo bar:baz];");
575   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
576
577   // Binary operators.
578   verifyFormat("[foo bar:baz], [foo bar:baz];");
579   verifyFormat("[foo bar:baz] = [foo bar:baz];");
580   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
581   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
582   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
583   verifyFormat("[foo bar:baz] += [foo bar:baz];");
584   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
585   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
586   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
587   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
588   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
589   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
590   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
591   verifyFormat("[foo bar:baz] || [foo bar:baz];");
592   verifyFormat("[foo bar:baz] && [foo bar:baz];");
593   verifyFormat("[foo bar:baz] | [foo bar:baz];");
594   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
595   verifyFormat("[foo bar:baz] & [foo bar:baz];");
596   verifyFormat("[foo bar:baz] == [foo bar:baz];");
597   verifyFormat("[foo bar:baz] != [foo bar:baz];");
598   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
599   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
600   verifyFormat("[foo bar:baz] > [foo bar:baz];");
601   verifyFormat("[foo bar:baz] < [foo bar:baz];");
602   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
603   verifyFormat("[foo bar:baz] << [foo bar:baz];");
604   verifyFormat("[foo bar:baz] - [foo bar:baz];");
605   verifyFormat("[foo bar:baz] + [foo bar:baz];");
606   verifyFormat("[foo bar:baz] * [foo bar:baz];");
607   verifyFormat("[foo bar:baz] / [foo bar:baz];");
608   verifyFormat("[foo bar:baz] % [foo bar:baz];");
609   // Whew!
610
611   verifyFormat("return in[42];");
612   verifyFormat("for (auto v : in[1]) {\n}");
613   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
614   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
615   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
616   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
617   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
618   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
619                "}");
620   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
621   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
622   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
623
624   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
625   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
626   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
627   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
628   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
629   verifyFormat("[button setAction:@selector(zoomOut:)];");
630   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
631
632   verifyFormat("arr[[self indexForFoo:a]];");
633   verifyFormat("throw [self errorFor:a];");
634   verifyFormat("@throw [self errorFor:a];");
635
636   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
637   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
638   verifyFormat("4 > 4 ? (id)a : (id)baz;");
639
640   // This tests that the formatter doesn't break after "backing" but before ":",
641   // which would be at 80 columns.
642   verifyFormat(
643       "void f() {\n"
644       "  if ((self = [super initWithContentRect:contentRect\n"
645       "                               styleMask:styleMask ?: otherMask\n"
646       "                                 backing:NSBackingStoreBuffered\n"
647       "                                   defer:YES]))");
648
649   verifyFormat(
650       "[foo checkThatBreakingAfterColonWorksOk:\n"
651       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
652
653   verifyFormat("[myObj short:arg1 // Force line break\n"
654                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
655                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
656                "                error:arg4];");
657   verifyFormat(
658       "void f() {\n"
659       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
660       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
661       "                                     pos.width(), pos.height())\n"
662       "                styleMask:NSBorderlessWindowMask\n"
663       "                  backing:NSBackingStoreBuffered\n"
664       "                    defer:NO]);\n"
665       "}");
666   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
667                "                             with:contentsNativeView];");
668
669   verifyFormat(
670       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
671       "           owner:nillllll];");
672
673   verifyFormat(
674       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
675       "        forType:kBookmarkButtonDragType];");
676
677   verifyFormat("[defaultCenter addObserver:self\n"
678                "                  selector:@selector(willEnterFullscreen)\n"
679                "                      name:kWillEnterFullscreenNotification\n"
680                "                    object:nil];");
681   verifyFormat("[image_rep drawInRect:drawRect\n"
682                "             fromRect:NSZeroRect\n"
683                "            operation:NSCompositeCopy\n"
684                "             fraction:1.0\n"
685                "       respectFlipped:NO\n"
686                "                hints:nil];");
687   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
688                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
689   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
690                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
691   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
692                "    aaaaaaaaaaaaaaaaaaaaaa];");
693
694   verifyFormat(
695       "scoped_nsobject<NSTextField> message(\n"
696       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
697       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
698   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
699                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
700                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
701                "          aaaa:bbb];");
702   verifyFormat("[self param:function( //\n"
703                "                parameter)]");
704   verifyFormat(
705       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
706       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
707       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
708
709   // Variadic parameters.
710   verifyFormat(
711       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
712   verifyFormat(
713       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
714       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
715       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
716   verifyFormat("[self // break\n"
717                "      a:a\n"
718                "    aaa:aaa];");
719   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
720                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
721
722   // Formats pair-parameters.
723   verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
724   verifyFormat("[I drawRectOn:surface //\n"
725                "       ofSize:aa:bbb\n"
726                "     atOrigin:cc:dd];");
727
728   // Inline block as a first argument.
729   verifyFormat("[object justBlock:^{\n"
730                "  a = 42;\n"
731                "}];");
732   verifyFormat("[object\n"
733                "    justBlock:^{\n"
734                "      a = 42;\n"
735                "    }\n"
736                "     notBlock:42\n"
737                "            a:42];");
738   verifyFormat("[object\n"
739                "    firstBlock:^{\n"
740                "      a = 42;\n"
741                "    }\n"
742                "    blockWithLongerName:^{\n"
743                "      a = 42;\n"
744                "    }];");
745   verifyFormat("[object\n"
746                "    blockWithLongerName:^{\n"
747                "      a = 42;\n"
748                "    }\n"
749                "    secondBlock:^{\n"
750                "      a = 42;\n"
751                "    }];");
752   verifyFormat("[object\n"
753                "    firstBlock:^{\n"
754                "      a = 42;\n"
755                "    }\n"
756                "    notBlock:42\n"
757                "    secondBlock:^{\n"
758                "      a = 42;\n"
759                "    }];");
760
761   Style.ColumnLimit = 70;
762   verifyFormat(
763       "void f() {\n"
764       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
765       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
766       "                                 pos.width(), pos.height())\n"
767       "                syeMask:NSBorderlessWindowMask\n"
768       "                  bking:NSBackingStoreBuffered\n"
769       "                    der:NO]);\n"
770       "}");
771
772   Style.ColumnLimit = 60;
773   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
774                "        .aaaaaaaa];"); // FIXME: Indentation seems off.
775   // FIXME: This violates the column limit.
776   verifyFormat(
777       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
778       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
779       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
780
781   Style = getChromiumStyle(FormatStyle::LK_ObjC);
782   Style.ColumnLimit = 80;
783   verifyFormat(
784       "void f() {\n"
785       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
786       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
787       "                                     pos.width(), pos.height())\n"
788       "                styleMask:NSBorderlessWindowMask\n"
789       "                  backing:NSBackingStoreBuffered\n"
790       "                    defer:NO]);\n"
791       "}");
792
793   // Respect continuation indent and colon alignment (e.g. when object name is
794   // short, and first selector is the longest one)
795   Style = getLLVMStyle();
796   Style.Language = FormatStyle::LK_ObjC;
797   Style.ContinuationIndentWidth = 8;
798   verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
799                "                       withObject:nil\n"
800                "                    waitUntilDone:false];");
801   verifyFormat("[self performSelector:@selector(loadAccessories)\n"
802                "        withObjectOnMainThread:nil\n"
803                "                 waitUntilDone:false];");
804   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
805                "        performSelectorOnMainThread:@selector(loadAccessories)\n"
806                "                         withObject:nil\n"
807                "                      waitUntilDone:false];");
808   verifyFormat("[self // force wrapping\n"
809                "        performSelectorOnMainThread:@selector(loadAccessories)\n"
810                "                         withObject:nil\n"
811                "                      waitUntilDone:false];");
812 }
813
814 TEST_F(FormatTestObjC, ObjCAt) {
815   verifyFormat("@autoreleasepool");
816   verifyFormat("@catch");
817   verifyFormat("@class");
818   verifyFormat("@compatibility_alias");
819   verifyFormat("@defs");
820   verifyFormat("@dynamic");
821   verifyFormat("@encode");
822   verifyFormat("@end");
823   verifyFormat("@finally");
824   verifyFormat("@implementation");
825   verifyFormat("@import");
826   verifyFormat("@interface");
827   verifyFormat("@optional");
828   verifyFormat("@package");
829   verifyFormat("@private");
830   verifyFormat("@property");
831   verifyFormat("@protected");
832   verifyFormat("@protocol");
833   verifyFormat("@public");
834   verifyFormat("@required");
835   verifyFormat("@selector");
836   verifyFormat("@synchronized");
837   verifyFormat("@synthesize");
838   verifyFormat("@throw");
839   verifyFormat("@try");
840
841   EXPECT_EQ("@interface", format("@ interface"));
842
843   // The precise formatting of this doesn't matter, nobody writes code like
844   // this.
845   verifyFormat("@ /*foo*/ interface");
846 }
847
848 TEST_F(FormatTestObjC, ObjCSnippets) {
849   verifyFormat("@autoreleasepool {\n"
850                "  foo();\n"
851                "}");
852   verifyFormat("@class Foo, Bar;");
853   verifyFormat("@compatibility_alias AliasName ExistingClass;");
854   verifyFormat("@dynamic textColor;");
855   verifyFormat("char *buf1 = @encode(int *);");
856   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
857   verifyFormat("char *buf1 = @encode(int **);");
858   verifyFormat("Protocol *proto = @protocol(p1);");
859   verifyFormat("SEL s = @selector(foo:);");
860   verifyFormat("@synchronized(self) {\n"
861                "  f();\n"
862                "}");
863
864   verifyFormat("@import foo.bar;\n"
865                "@import baz;");
866
867   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
868
869   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
870   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
871
872   Style = getMozillaStyle();
873   verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
874   verifyFormat("@property BOOL editable;");
875
876   Style = getWebKitStyle();
877   verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
878   verifyFormat("@property BOOL editable;");
879
880   Style = getGoogleStyle(FormatStyle::LK_ObjC);
881   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
882   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
883 }
884
885 TEST_F(FormatTestObjC, ObjCForIn) {
886   verifyFormat("- (void)test {\n"
887                "  for (NSString *n in arrayOfStrings) {\n"
888                "    foo(n);\n"
889                "  }\n"
890                "}");
891   verifyFormat("- (void)test {\n"
892                "  for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
893                "    foo(n);\n"
894                "  }\n"
895                "}");
896   verifyFormat("for (Foo *x in bar) {\n}");
897   verifyFormat("for (Foo *x in [bar baz]) {\n}");
898   verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
899   verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
900   verifyFormat("for (Foo *x in [bar baz:^{\n"
901                "       [uh oh];\n"
902                "     }]) {\n}");
903 }
904
905 TEST_F(FormatTestObjC, ObjCLiterals) {
906   verifyFormat("@\"String\"");
907   verifyFormat("@1");
908   verifyFormat("@+4.8");
909   verifyFormat("@-4");
910   verifyFormat("@1LL");
911   verifyFormat("@.5");
912   verifyFormat("@'c'");
913   verifyFormat("@true");
914
915   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
916   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
917   verifyFormat("NSNumber *favoriteColor = @(Green);");
918   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
919
920   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
921 }
922
923 TEST_F(FormatTestObjC, ObjCDictLiterals) {
924   verifyFormat("@{");
925   verifyFormat("@{}");
926   verifyFormat("@{@\"one\" : @1}");
927   verifyFormat("return @{@\"one\" : @1;");
928   verifyFormat("@{@\"one\" : @1}");
929
930   verifyFormat("@{@\"one\" : @{@2 : @1}}");
931   verifyFormat("@{\n"
932                "  @\"one\" : @{@2 : @1},\n"
933                "}");
934
935   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
936   verifyIncompleteFormat("[self setDict:@{}");
937   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
938   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
939   verifyFormat(
940       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
941   verifyFormat(
942       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
943
944   verifyFormat("NSDictionary *d = @{\n"
945                "  @\"nam\" : NSUserNam(),\n"
946                "  @\"dte\" : [NSDate date],\n"
947                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
948                "};");
949   verifyFormat(
950       "@{\n"
951       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
952       "regularFont,\n"
953       "};");
954   verifyFormat(
955       "@{\n"
956       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
957       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
958       "};");
959
960   // We should try to be robust in case someone forgets the "@".
961   verifyFormat("NSDictionary *d = {\n"
962                "  @\"nam\" : NSUserNam(),\n"
963                "  @\"dte\" : [NSDate date],\n"
964                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
965                "};");
966   verifyFormat("NSMutableDictionary *dictionary =\n"
967                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
968                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
969                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
970                "      cccccccccccccccc : ccccccccccccccc\n"
971                "    }];");
972
973   // Ensure that casts before the key are kept on the same line as the key.
974   verifyFormat(
975       "NSDictionary *d = @{\n"
976       "  (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
977       "  (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
978       "};");
979
980   Style = getGoogleStyle(FormatStyle::LK_ObjC);
981   verifyFormat(
982       "@{\n"
983       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
984       "regularFont,\n"
985       "};");
986 }
987
988 TEST_F(FormatTestObjC, ObjCArrayLiterals) {
989   verifyIncompleteFormat("@[");
990   verifyFormat("@[]");
991   verifyFormat(
992       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
993   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
994   verifyFormat("NSArray *array = @[ [foo description] ];");
995
996   verifyFormat(
997       "NSArray *some_variable = @[\n"
998       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
999       "  @\"aaaaaaaaaaaaaaaaa\",\n"
1000       "  @\"aaaaaaaaaaaaaaaaa\",\n"
1001       "  @\"aaaaaaaaaaaaaaaaa\",\n"
1002       "];");
1003   verifyFormat(
1004       "NSArray *some_variable = @[\n"
1005       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1006       "  @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1007       "];");
1008   verifyFormat("NSArray *some_variable = @[\n"
1009                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1010                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1011                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1012                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1013                "];");
1014   verifyFormat("NSArray *array = @[\n"
1015                "  @\"a\",\n"
1016                "  @\"a\",\n" // Trailing comma -> one per line.
1017                "];");
1018
1019   // We should try to be robust in case someone forgets the "@".
1020   verifyFormat("NSArray *some_variable = [\n"
1021                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1022                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1023                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1024                "  @\"aaaaaaaaaaaaaaaaa\",\n"
1025                "];");
1026   verifyFormat(
1027       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1028       "                                             index:(NSUInteger)index\n"
1029       "                                nonDigitAttributes:\n"
1030       "                                    (NSDictionary *)noDigitAttributes;");
1031   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1032                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1033                "]];");
1034   Style.ColumnLimit = 20;
1035   // We can't break string literals inside NSArray literals
1036   // (that raises -Wobjc-string-concatenation).
1037   verifyFormat("NSArray *foo = @[\n"
1038                "  @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1039                "];\n");
1040 }
1041 } // end namespace
1042 } // end namespace format
1043 } // end namespace clang