Debian Codegraph @

clang/unittests/Format/FormatTestObjC.cpp

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