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
13namespace clang {
14namespace format {
15namespace test {
16namespace {
17
18class FormatTestObjC : public FormatTestBase {
19protected:
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
34TEST(FormatTestObjCStyle, DetectsObjCInStdin) {
35 auto Style = getStyle(StyleName: "LLVM", FileName: "<stdin>", FallbackStyle: "none",
36 Code: "@interface\n"
37 "- (id)init;");
38 ASSERT_TRUE((bool)Style);
39 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
40}
41
42TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
43 auto Style = getStyle(StyleName: "LLVM", FileName: "a.h", FallbackStyle: "none",
44 Code: "@interface\n"
45 "- (id)init;");
46 ASSERT_TRUE((bool)Style);
47 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
48
49 Style = getStyle(StyleName: "LLVM", FileName: "a.h", FallbackStyle: "none",
50 Code: "@interface\n"
51 "+ (id)init;");
52 ASSERT_TRUE((bool)Style);
53 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
54
55 Style = getStyle(StyleName: "LLVM", FileName: "a.h", FallbackStyle: "none",
56 Code: "@interface\n"
57 "@end\n"
58 "//comment");
59 ASSERT_TRUE((bool)Style);
60 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
61
62 Style = getStyle(StyleName: "LLVM", FileName: "a.h", FallbackStyle: "none",
63 Code: "@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(StyleName: "LLVM", FileName: "a.h", FallbackStyle: "none", Code: "void f() {}");
70 ASSERT_TRUE((bool)Style);
71 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
72
73 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "@interface Foo\n@end");
74 ASSERT_TRUE((bool)Style);
75 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
76
77 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none",
78 Code: "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(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "@protocol Foo\n@end");
83 ASSERT_TRUE((bool)Style);
84 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
85
86 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none",
87 Code: "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(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "typedef NS_ENUM(int, Foo) {};");
92 ASSERT_TRUE((bool)Style);
93 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
94
95 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "typedef NS_CLOSED_ENUM(int, Foo) {};");
96 ASSERT_TRUE((bool)Style);
97 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
98
99 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "typedef NS_ERROR_ENUM(int, Foo) {};");
100 ASSERT_TRUE((bool)Style);
101 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
102
103 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: R"objc(
104NS_ASSUME_NONNULL_BEGIN
105extern int i;
106NS_ASSUME_NONNULL_END
107)objc");
108 ASSERT_TRUE((bool)Style);
109 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
110
111 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: R"objc(
112FOUNDATION_EXTERN void DoStuff(void);
113)objc");
114 ASSERT_TRUE((bool)Style);
115 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
116
117 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: R"objc(
118FOUNDATION_EXPORT void DoStuff(void);
119)objc");
120 ASSERT_TRUE((bool)Style);
121 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
122
123 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "enum Foo {};");
124 ASSERT_TRUE((bool)Style);
125 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
126
127 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "inline void Foo() { Log(@\"Foo\"); }");
128 ASSERT_TRUE((bool)Style);
129 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
130
131 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "inline void Foo() { Log(\"Foo\"); }");
132 ASSERT_TRUE((bool)Style);
133 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
134
135 Style =
136 getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "inline void Foo() { id = @[1, 2, 3]; }");
137 ASSERT_TRUE((bool)Style);
138 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
139
140 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none",
141 Code: "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(StyleName: "{}", FileName: "a.h", FallbackStyle: "none",
146 Code: "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(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "extern NSString *kFoo;");
152 ASSERT_TRUE((bool)Style);
153 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
154
155 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "extern NSInteger Foo();");
156 ASSERT_TRUE((bool)Style);
157 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
158
159 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "NSObject *Foo();");
160 ASSERT_TRUE((bool)Style);
161 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
162
163 Style = getStyle(StyleName: "{}", FileName: "a.h", FallbackStyle: "none", Code: "NSSet *Foo();");
164 ASSERT_TRUE((bool)Style);
165 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
166}
167
168TEST(FormatTestObjCStyle, AvoidDetectingDesignatedInitializersAsObjCInHeaders) {
169 auto Style = getStyle(StyleName: "LLVM", FileName: "a.h", FallbackStyle: "none",
170 Code: "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(StyleName: "LLVM", FileName: "a.h", FallbackStyle: "none",
176 Code: "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
182TEST_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
197TEST_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
216TEST_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
227TEST_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
246TEST_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(Language: 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
430TEST_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(Language: 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
507TEST_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(Language: FormatStyle::LK_ObjC);
549 verifyFormat("@protocol MyProtocol <NSObject>\n"
550 "- (NSUInteger)numberOfThings;\n"
551 "@end");
552}
553
554TEST_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(Language: 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(Language: 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
635TEST_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 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
767 " with:contentsNativeView];");
768
769 verifyFormat(
770 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
771 " owner:nillllll];");
772
773 verifyFormat(
774 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
775 " forType:kBookmarkButtonDragType];");
776
777 verifyFormat("[defaultCenter addObserver:self\n"
778 " selector:@selector(willEnterFullscreen)\n"
779 " name:kWillEnterFullscreenNotification\n"
780 " object:nil];");
781 verifyFormat("[image_rep drawInRect:drawRect\n"
782 " fromRect:NSZeroRect\n"
783 " operation:NSCompositeCopy\n"
784 " fraction:1.0\n"
785 " respectFlipped:NO\n"
786 " hints:nil];");
787 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
789 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
791 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
792 " aaaaaaaaaaaaaaaaaaaaaa];");
793
794 verifyFormat(
795 "scoped_nsobject<NSTextField> message(\n"
796 " // The frame will be fixed up when |-setMessageText:| is called.\n"
797 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
798 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
799 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
800 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
801 " aaaa:bbb];");
802 verifyFormat("[self param:function( //\n"
803 " parameter)]");
804 verifyFormat(
805 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
806 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
807 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
808
809 // Variadic parameters.
810 verifyFormat(
811 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
812 verifyFormat(
813 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
814 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
815 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
816
817 verifyFormat("[self // break\n"
818 " a:a\n"
819 " aaa:aaa];");
820
821 // Formats pair-parameters.
822 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
823 verifyFormat("[I drawRectOn:surface //\n"
824 " ofSize:aa:bbb\n"
825 " atOrigin:cc:dd];");
826
827 // Inline block as a first argument.
828 verifyFormat("[object justBlock:^{\n"
829 " a = 42;\n"
830 "}];");
831 verifyFormat("[object\n"
832 " justBlock:^{\n"
833 " a = 42;\n"
834 " }\n"
835 " notBlock:42\n"
836 " a:42];");
837 verifyFormat("[object\n"
838 " firstBlock:^{\n"
839 " a = 42;\n"
840 " }\n"
841 " blockWithLongerName:^{\n"
842 " a = 42;\n"
843 " }];");
844 verifyFormat("[object\n"
845 " blockWithLongerName:^{\n"
846 " a = 42;\n"
847 " }\n"
848 " secondBlock:^{\n"
849 " a = 42;\n"
850 " }];");
851 verifyFormat("[object\n"
852 " firstBlock:^{\n"
853 " a = 42;\n"
854 " }\n"
855 " notBlock:42\n"
856 " secondBlock:^{\n"
857 " a = 42;\n"
858 " }];");
859
860 // Space between cast rparen and selector name component.
861 verifyFormat("[((Foo *)foo) bar];");
862 verifyFormat("[((Foo *)foo) bar:1 blech:2];");
863
864 Style.ColumnLimit = 20;
865 verifyFormat("aaaaa = [a aa:aa\n"
866 " aa:aa];");
867 verifyFormat("aaaaaa = [aa aa:aa\n"
868 " aa:aa];");
869
870 // Message receiver taking multiple lines.
871 // Non-corner case.
872 verifyFormat("[[object block:^{\n"
873 " return 42;\n"
874 "}] a:42 b:42];");
875 // Arguments just fit into one line.
876 verifyFormat("[[object block:^{\n"
877 " return 42;\n"
878 "}] aaaaaaa:42 b:42];");
879 // Arguments just over a column limit.
880 verifyFormat("[[object block:^{\n"
881 " return 42;\n"
882 "}] aaaaaaa:42\n"
883 " bb:42];");
884 // Arguments just fit into one line.
885 Style.ColumnLimit = 23;
886 verifyFormat("[[obj a:42\n"
887 " b:42\n"
888 " c:42\n"
889 " d:42] e:42 f:42];");
890
891 // Arguments do not fit into one line with a receiver.
892 Style.ColumnLimit = 20;
893 verifyFormat("[[obj a:42] a:42\n"
894 " b:42];");
895 verifyFormat("[[obj a:42] a:42\n"
896 " b:42\n"
897 " c:42];");
898 verifyFormat("[[obj aaaaaa:42\n"
899 " b:42]\n"
900 " cc:42\n"
901 " d:42];");
902
903 // Avoid breaking receiver expression.
904 Style.ColumnLimit = 30;
905 verifyFormat("fooooooo =\n"
906 " [[obj fooo] aaa:42\n"
907 " aaa:42];");
908 verifyFormat("[[[obj foo] bar] aa:42\n"
909 " bb:42\n"
910 " cc:42];");
911
912 // Avoid breaking between unary operators and ObjC method expressions.
913 Style.ColumnLimit = 45;
914 verifyFormat("if (a012345678901234567890123 &&\n"
915 " ![foo bar]) {\n"
916 "}");
917 verifyFormat("if (a012345678901234567890123 &&\n"
918 " +[foo bar]) {\n"
919 "}");
920 verifyFormat("if (a012345678901234567890123 &&\n"
921 " -[foo bar]) {\n"
922 "}");
923
924 Style.ColumnLimit = 70;
925 verifyFormat(
926 "void f() {\n"
927 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
928 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
929 " pos.width(), pos.height())\n"
930 " syeMask:NSBorderlessWindowMask\n"
931 " bking:NSBackingStoreBuffered\n"
932 " der:NO]);\n"
933 "}");
934
935 Style.ColumnLimit = 60;
936 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
937 " .aaaaaaaa];"); // FIXME: Indentation seems off.
938 // FIXME: This violates the column limit.
939 verifyFormat(
940 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
941 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
942 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
943
944 Style = getChromiumStyle(Language: FormatStyle::LK_ObjC);
945 Style.ColumnLimit = 80;
946 verifyFormat(
947 "void f() {\n"
948 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
949 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
950 " pos.width(), pos.height())\n"
951 " styleMask:NSBorderlessWindowMask\n"
952 " backing:NSBackingStoreBuffered\n"
953 " defer:NO]);\n"
954 "}");
955
956 // Respect continuation indent and colon alignment (e.g. when object name is
957 // short, and first selector is the longest one)
958 Style = getLLVMStyle();
959 Style.Language = FormatStyle::LK_ObjC;
960 Style.ContinuationIndentWidth = 8;
961 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
962 " withObject:nil\n"
963 " waitUntilDone:false];");
964 verifyFormat("[self performSelector:@selector(loadAccessories)\n"
965 " withObjectOnMainThread:nil\n"
966 " waitUntilDone:false];");
967 verifyFormat(
968 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
969 " performSelectorOnMainThread:@selector(loadAccessories)\n"
970 " withObject:nil\n"
971 " waitUntilDone:false];");
972 verifyFormat(
973 "[self // force wrapping\n"
974 " performSelectorOnMainThread:@selector(loadAccessories)\n"
975 " withObject:nil\n"
976 " waitUntilDone:false];");
977
978 // The appropriate indentation is used after a block statement.
979 Style.ContinuationIndentWidth = 4;
980 verifyFormat(
981 "void aaaaaaaaaaaaaaaaaaaaa(int c) {\n"
982 " if (c) {\n"
983 " f();\n"
984 " }\n"
985 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd\n"
986 " eeeeeeeeeeeeeeeeeeeeeeeeeeeee:^(fffffffffffffff gggggggg) {\n"
987 " f(SSSSS, c);\n"
988 " }];\n"
989 "}");
990}
991
992TEST_F(FormatTestObjC, ObjCAt) {
993 verifyFormat("@autoreleasepool");
994 verifyFormat("@catch");
995 verifyFormat("@class");
996 verifyFormat("@compatibility_alias");
997 verifyFormat("@defs");
998 verifyFormat("@dynamic");
999 verifyFormat("@encode");
1000 verifyFormat("@end");
1001 verifyFormat("@finally");
1002 verifyFormat("@implementation");
1003 verifyFormat("@import");
1004 verifyFormat("@interface");
1005 verifyFormat("@optional");
1006 verifyFormat("@package");
1007 verifyFormat("@private");
1008 verifyFormat("@property");
1009 verifyFormat("@protected");
1010 verifyFormat("@protocol");
1011 verifyFormat("@public");
1012 verifyFormat("@required");
1013 verifyFormat("@selector");
1014 verifyFormat("@synchronized");
1015 verifyFormat("@synthesize");
1016 verifyFormat("@throw");
1017 verifyFormat("@try");
1018
1019 EXPECT_EQ("@interface", format("@ interface"));
1020
1021 // The precise formatting of this doesn't matter, nobody writes code like
1022 // this.
1023 verifyFormat("@ /*foo*/ interface");
1024}
1025
1026TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) {
1027 verifyFormat("void DoStuffWithBlockType(int (^)(char));");
1028 verifyFormat("int (^foo)(char, float);");
1029 verifyFormat("int (^foo[10])(char, float);");
1030 verifyFormat("int (^foo[kNumEntries])(char, float);");
1031 verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
1032 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
1033
1034 verifyFormat("int *p = ^int *() { //\n"
1035 " return nullptr;\n"
1036 "}();");
1037
1038 verifyFormat("int * (^p)(void) = ^int *(void) { //\n"
1039 " return nullptr;\n"
1040 "};");
1041
1042 // WebKit forces function braces onto a newline, but blocks should not.
1043 verifyFormat("int* p = ^int*() { //\n"
1044 " return nullptr;\n"
1045 "}();",
1046 getWebKitStyle());
1047}
1048
1049TEST_F(FormatTestObjC, ObjCSnippets) {
1050 verifyFormat("@autoreleasepool {\n"
1051 " foo();\n"
1052 "}");
1053 verifyFormat("@class Foo, Bar;");
1054 verifyFormat("@compatibility_alias AliasName ExistingClass;");
1055 verifyFormat("@dynamic textColor;");
1056 verifyFormat("char *buf1 = @encode(int *);");
1057 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1058 verifyFormat("char *buf1 = @encode(int **);");
1059 verifyFormat("Protocol *proto = @protocol(p1);");
1060 verifyFormat("SEL s = @selector(foo:);");
1061 verifyFormat("@synchronized(self) {\n"
1062 " f();\n"
1063 "}");
1064
1065 verifyFormat("@import foo.bar;\n"
1066 "@import baz;");
1067
1068 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1069
1070 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
1071 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1072
1073 verifyFormat("extern UIWindow *MainWindow(void) "
1074 "NS_SWIFT_NAME(getter:MyHelper.mainWindow());");
1075
1076 verifyFormat("extern UIWindow *MainWindow(void) "
1077 "CF_SWIFT_NAME(getter:MyHelper.mainWindow());");
1078
1079 Style.ColumnLimit = 50;
1080 verifyFormat("@interface Foo\n"
1081 "- (void)doStuffWithFoo:(id)name\n"
1082 " bar:(id)bar\n"
1083 " baz:(id)baz\n"
1084 " NS_SWIFT_NAME(doStuff(withFoo:bar:baz:));\n"
1085 "@end");
1086
1087 Style = getMozillaStyle();
1088 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
1089 verifyFormat("@property BOOL editable;");
1090
1091 Style = getWebKitStyle();
1092 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
1093 verifyFormat("@property BOOL editable;");
1094
1095 Style = getGoogleStyle(Language: FormatStyle::LK_ObjC);
1096 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1097 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1098}
1099
1100TEST_F(FormatTestObjC, ObjCForIn) {
1101 verifyFormat("- (void)test {\n"
1102 " for (NSString *n in arrayOfStrings) {\n"
1103 " foo(n);\n"
1104 " }\n"
1105 "}");
1106 verifyFormat("- (void)test {\n"
1107 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
1108 " foo(n);\n"
1109 " }\n"
1110 "}");
1111 verifyFormat("for (Foo *x in bar) {\n}");
1112 verifyFormat("for (Foo *x in [bar baz]) {\n}");
1113 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
1114 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
1115 verifyFormat("for (Foo *x in [bar baz:^{\n"
1116 " [uh oh];\n"
1117 " }]) {\n}");
1118}
1119
1120TEST_F(FormatTestObjC, ObjCCxxKeywords) {
1121 verifyFormat("+ (instancetype)new {\n"
1122 " return nil;\n"
1123 "}");
1124 verifyFormat("+ (instancetype)myNew {\n"
1125 " return [self new];\n"
1126 "}");
1127 verifyFormat("SEL NewSelector(void) { return @selector(new); }");
1128 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }");
1129 verifyFormat("+ (instancetype)delete {\n"
1130 " return nil;\n"
1131 "}");
1132 verifyFormat("+ (instancetype)myDelete {\n"
1133 " return [self delete];\n"
1134 "}");
1135 verifyFormat("SEL DeleteSelector(void) { return @selector(delete); }");
1136 verifyFormat("SEL MacroSelector(void) { return MACRO(delete); }");
1137 verifyFormat("MACRO(new:)");
1138 verifyFormat("MACRO(delete:)");
1139 verifyFormat("foo = @{MACRO(new:) : MACRO(delete:)}");
1140 verifyFormat("@implementation Foo\n"
1141 "// Testing\n"
1142 "- (Class)class {\n"
1143 "}\n"
1144 "- (void)foo {\n"
1145 "}\n"
1146 "@end");
1147 verifyFormat("@implementation Foo\n"
1148 "- (Class)class {\n"
1149 "}\n"
1150 "- (void)foo {\n"
1151 "}\n"
1152 "@end");
1153 verifyFormat("@implementation Foo\n"
1154 "+ (Class)class {\n"
1155 "}\n"
1156 "- (void)foo {\n"
1157 "}\n"
1158 "@end");
1159 verifyFormat("@implementation Foo\n"
1160 "- (Class)class:(Class)klass {\n"
1161 "}\n"
1162 "- (void)foo {\n"
1163 "}\n"
1164 "@end");
1165 verifyFormat("@implementation Foo\n"
1166 "+ (Class)class:(Class)klass {\n"
1167 "}\n"
1168 "- (void)foo {\n"
1169 "}\n"
1170 "@end");
1171
1172 verifyFormat("@interface Foo\n"
1173 "// Testing\n"
1174 "- (Class)class;\n"
1175 "- (void)foo;\n"
1176 "@end");
1177 verifyFormat("@interface Foo\n"
1178 "- (Class)class;\n"
1179 "- (void)foo;\n"
1180 "@end");
1181 verifyFormat("@interface Foo\n"
1182 "+ (Class)class;\n"
1183 "- (void)foo;\n"
1184 "@end");
1185 verifyFormat("@interface Foo\n"
1186 "- (Class)class:(Class)klass;\n"
1187 "- (void)foo;\n"
1188 "@end");
1189 verifyFormat("@interface Foo\n"
1190 "+ (Class)class:(Class)klass;\n"
1191 "- (void)foo;\n"
1192 "@end");
1193}
1194
1195TEST_F(FormatTestObjC, ObjCLiterals) {
1196 verifyFormat("@\"String\"");
1197 verifyFormat("@1");
1198 verifyFormat("@+4.8");
1199 verifyFormat("@-4");
1200 verifyFormat("@1LL");
1201 verifyFormat("@.5");
1202 verifyFormat("@'c'");
1203 verifyFormat("@true");
1204
1205 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
1206 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
1207 verifyFormat("NSNumber *favoriteColor = @(Green);");
1208 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
1209
1210 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
1211}
1212
1213TEST_F(FormatTestObjC, ObjCDictLiterals) {
1214 verifyFormat("@{");
1215 verifyFormat("@{}");
1216 verifyFormat("@{@\"one\" : @1}");
1217 verifyFormat("return @{@\"one\" : @1;");
1218 verifyFormat("@{@\"one\" : @1}");
1219
1220 verifyFormat("@{@\"one\" : @{@2 : @1}}");
1221 verifyFormat("@{\n"
1222 " @\"one\" : @{@2 : @1},\n"
1223 "}");
1224
1225 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
1226 verifyIncompleteFormat("[self setDict:@{}");
1227 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
1228 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
1229 verifyFormat(
1230 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
1231 verifyFormat(
1232 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
1233
1234 verifyFormat("NSDictionary *d = @{\n"
1235 " @\"nam\" : NSUserNam(),\n"
1236 " @\"dte\" : [NSDate date],\n"
1237 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1238 "};");
1239 verifyFormat(
1240 "@{\n"
1241 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1242 "regularFont,\n"
1243 "};");
1244 verifyFormat(
1245 "@{\n"
1246 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
1247 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
1248 "};");
1249
1250 // We should try to be robust in case someone forgets the "@".
1251 verifyFormat("NSDictionary *d = {\n"
1252 " @\"nam\" : NSUserNam(),\n"
1253 " @\"dte\" : [NSDate date],\n"
1254 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1255 "};");
1256 verifyFormat("NSMutableDictionary *dictionary =\n"
1257 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
1258 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
1259 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
1260 " cccccccccccccccc : ccccccccccccccc\n"
1261 " }];");
1262
1263 // Ensure that casts before the key are kept on the same line as the key.
1264 verifyFormat(
1265 "NSDictionary *d = @{\n"
1266 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1267 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
1268 "};");
1269 Style.ColumnLimit = 40;
1270 verifyFormat("int Foo() {\n"
1271 " a12345 = @{a12345 : a12345};\n"
1272 "}");
1273 verifyFormat("int Foo() {\n"
1274 " a12345 = @{a12345 : @(a12345)};\n"
1275 "}");
1276 verifyFormat("int Foo() {\n"
1277 " a12345 = @{(Foo *)a12345 : @(a12345)};\n"
1278 "}");
1279 verifyFormat("int Foo() {\n"
1280 " a12345 = @{@(a12345) : a12345};\n"
1281 "}");
1282 verifyFormat("int Foo() {\n"
1283 " a12345 = @{@(a12345) : @YES};\n"
1284 "}");
1285 Style.SpacesInContainerLiterals = false;
1286 verifyFormat("int Foo() {\n"
1287 " b12345 = @{b12345: b12345};\n"
1288 "}");
1289 verifyFormat("int Foo() {\n"
1290 " b12345 = @{(Foo *)b12345: @(b12345)};\n"
1291 "}");
1292 Style.SpacesInContainerLiterals = true;
1293
1294 Style = getGoogleStyle(Language: FormatStyle::LK_ObjC);
1295 verifyFormat(
1296 "@{\n"
1297 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1298 "regularFont,\n"
1299 "};");
1300}
1301
1302TEST_F(FormatTestObjC, ObjCArrayLiterals) {
1303 verifyIncompleteFormat("@[");
1304 verifyFormat("@[]");
1305 verifyFormat(
1306 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
1307 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
1308 verifyFormat("NSArray *array = @[ [foo description] ];");
1309
1310 verifyFormat(
1311 "NSArray *some_variable = @[\n"
1312 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1313 " @\"aaaaaaaaaaaaaaaaa\",\n"
1314 " @\"aaaaaaaaaaaaaaaaa\",\n"
1315 " @\"aaaaaaaaaaaaaaaaa\",\n"
1316 "];");
1317 verifyFormat(
1318 "NSArray *some_variable = @[\n"
1319 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1320 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1321 "];");
1322 verifyFormat("NSArray *some_variable = @[\n"
1323 " @\"aaaaaaaaaaaaaaaaa\",\n"
1324 " @\"aaaaaaaaaaaaaaaaa\",\n"
1325 " @\"aaaaaaaaaaaaaaaaa\",\n"
1326 " @\"aaaaaaaaaaaaaaaaa\",\n"
1327 "];");
1328 verifyFormat("NSArray *array = @[\n"
1329 " @\"a\",\n"
1330 " @\"a\",\n" // Trailing comma -> one per line.
1331 "];");
1332
1333 // We should try to be robust in case someone forgets the "@".
1334 verifyFormat("NSArray *some_variable = [\n"
1335 " @\"aaaaaaaaaaaaaaaaa\",\n"
1336 " @\"aaaaaaaaaaaaaaaaa\",\n"
1337 " @\"aaaaaaaaaaaaaaaaa\",\n"
1338 " @\"aaaaaaaaaaaaaaaaa\",\n"
1339 "];");
1340 verifyFormat(
1341 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1342 " index:(NSUInteger)index\n"
1343 " nonDigitAttributes:\n"
1344 " (NSDictionary *)noDigitAttributes;");
1345 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1346 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1347 "]];");
1348 Style.ColumnLimit = 40;
1349 verifyFormat("int Foo() {\n"
1350 " a12345 = @[ a12345, a12345 ];\n"
1351 "}");
1352 verifyFormat("int Foo() {\n"
1353 " a123 = @[ (Foo *)a12345, @(a12345) ];\n"
1354 "}");
1355 Style.SpacesInContainerLiterals = false;
1356 verifyFormat("int Foo() {\n"
1357 " b12345 = @[b12345, b12345];\n"
1358 "}");
1359 verifyFormat("int Foo() {\n"
1360 " b12345 = @[(Foo *)b12345, @(b12345)];\n"
1361 "}");
1362 Style.SpacesInContainerLiterals = true;
1363 Style.ColumnLimit = 20;
1364 // We can't break string literals inside NSArray literals
1365 // (that raises -Wobjc-string-concatenation).
1366 verifyFormat("NSArray *foo = @[\n"
1367 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1368 "];");
1369}
1370
1371TEST_F(FormatTestObjC, BreaksCallStatementWhereSemiJustOverTheLimit) {
1372 Style.ColumnLimit = 60;
1373 // If the statement starting with 'a = ...' is put on a single line, the ';'
1374 // is at line 61.
1375 verifyFormat("int f(int a) {\n"
1376 " a = [self aaaaaaaaaa:bbbbbbbbb\n"
1377 " ccccccccc:dddddddd\n"
1378 " ee:fddd];\n"
1379 "}");
1380}
1381
1382TEST_F(FormatTestObjC, AlwaysBreakBeforeMultilineStrings) {
1383 Style = getGoogleStyle(Language: FormatStyle::LK_ObjC);
1384 Style.ColumnLimit = 40;
1385 verifyFormat("aaaa = @\"bbbb\"\n"
1386 " @\"cccc\";");
1387 verifyFormat("aaaa(@\"bbbb\"\n"
1388 " @\"cccc\");");
1389 verifyFormat("aaaa(qqq, @\"bbbb\"\n"
1390 " @\"cccc\");");
1391 verifyFormat("[aaaa qqqq:@\"bbbb\"\n"
1392 " @\"cccc\"];");
1393 verifyFormat("aaaa = [aaaa qqqq:@\"bbbb\"\n"
1394 " @\"cccc\"];");
1395 verifyFormat("[aaaa qqqq:@\"bbbb\"\n"
1396 " @\"cccc\"\n"
1397 " rr:42\n"
1398 " ssssss:@\"ee\"\n"
1399 " @\"fffff\"];");
1400}
1401
1402TEST_F(FormatTestObjC, DisambiguatesCallsFromCppLambdas) {
1403 verifyFormat("x = ([a foo:bar] && b->c == 'd');");
1404 verifyFormat("x = ([a foo:bar] + b->c == 'd');");
1405 verifyFormat("x = ([a foo:bar] + !b->c == 'd');");
1406 verifyFormat("x = ([a foo:bar] + ~b->c == 'd');");
1407 verifyFormat("x = ([a foo:bar] - b->c == 'd');");
1408 verifyFormat("x = ([a foo:bar] / b->c == 'd');");
1409 verifyFormat("x = ([a foo:bar] % b->c == 'd');");
1410 verifyFormat("x = ([a foo:bar] | b->c == 'd');");
1411 verifyFormat("x = ([a foo:bar] || b->c == 'd');");
1412 verifyFormat("x = ([a foo:bar] && b->c == 'd');");
1413 verifyFormat("x = ([a foo:bar] == b->c == 'd');");
1414 verifyFormat("x = ([a foo:bar] != b->c == 'd');");
1415 verifyFormat("x = ([a foo:bar] <= b->c == 'd');");
1416 verifyFormat("x = ([a foo:bar] >= b->c == 'd');");
1417 verifyFormat("x = ([a foo:bar] << b->c == 'd');");
1418 verifyFormat("x = ([a foo:bar] ? b->c == 'd' : 'e');");
1419 // FIXME: The following are wrongly classified as C++ lambda expressions.
1420 // For example this code:
1421 // x = ([a foo:bar] & b->c == 'd');
1422 // is formatted as:
1423 // x = ([a foo:bar] & b -> c == 'd');
1424 // verifyFormat("x = ([a foo:bar] & b->c == 'd');");
1425 // verifyFormat("x = ([a foo:bar] > b->c == 'd');");
1426 // verifyFormat("x = ([a foo:bar] < b->c == 'd');");
1427 // verifyFormat("x = ([a foo:bar] >> b->c == 'd');");
1428}
1429
1430TEST_F(FormatTestObjC, DisambiguatesCallsFromStructuredBindings) {
1431 verifyFormat("int f() {\n"
1432 " if (a && [f arg])\n"
1433 " return 0;\n"
1434 "}");
1435 verifyFormat("int f() {\n"
1436 " if (a & [f arg])\n"
1437 " return 0;\n"
1438 "}");
1439 verifyFormat("int f() {\n"
1440 " for (auto &[elem] : list)\n"
1441 " return 0;\n"
1442 "}");
1443 verifyFormat("int f() {\n"
1444 " for (auto &&[elem] : list)\n"
1445 " return 0;\n"
1446 "}");
1447 verifyFormat(
1448 "int f() {\n"
1449 " for (auto /**/ const /**/ volatile /**/ && /**/ [elem] : list)\n"
1450 " return 0;\n"
1451 "}");
1452}
1453
1454TEST_F(FormatTestObjC, BreakLineBeforeNestedBlockParam) {
1455 Style = getGoogleStyle(Language: FormatStyle::LK_ObjC);
1456 Style.ObjCBreakBeforeNestedBlockParam = false;
1457 Style.ColumnLimit = 0;
1458
1459 verifyFormat("[self.test1 t:self callback:^(typeof(self) self, NSNumber *u, "
1460 "NSNumber *v) {\n"
1461 " u = v;\n"
1462 "}]");
1463
1464 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1465 "NSNumber *u, NSNumber *v) {\n"
1466 " u = v;\n"
1467 "}]");
1468
1469 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1470 "NSNumber *u, NSNumber *v) {\n"
1471 " u = c;\n"
1472 "} w:self callback2:^(typeof(self) self, NSNumber *a, NSNumber "
1473 "*b, NSNumber *c) {\n"
1474 " b = c;\n"
1475 "}]");
1476 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1477 "NSNumber *u, NSNumber *v) {\n"
1478 " u = v;\n"
1479 "} z:self]");
1480
1481 Style.ColumnLimit = 80;
1482 verifyFormat(
1483 "[self.test_method a:self b:self\n"
1484 " callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {\n"
1485 " u = v;\n"
1486 " }]");
1487
1488 verifyFormat("[self block:^(void) {\n"
1489 " doStuff();\n"
1490 "} completionHandler:^(void) {\n"
1491 " doStuff();\n"
1492 " [self block:^(void) {\n"
1493 " doStuff();\n"
1494 " } completionHandler:^(void) {\n"
1495 " doStuff();\n"
1496 " }];\n"
1497 "}];");
1498
1499 Style.ColumnLimit = 0;
1500 verifyFormat("[[SessionService sharedService] "
1501 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
1502 " if (window) {\n"
1503 " [self windowDidLoad:window];\n"
1504 " } else {\n"
1505 " [self errorLoadingWindow];\n"
1506 " }\n"
1507 "}];");
1508 verifyFormat("[controller test:^{\n"
1509 " doStuff();\n"
1510 "} withTimeout:5 completionHandler:^{\n"
1511 " doStuff();\n"
1512 "}];");
1513 verifyFormat(
1514 "[self setupTextFieldSignals:@[\n"
1515 " self.documentWidthField,\n"
1516 " self.documentHeightField,\n"
1517 "] solver:^(NSTextField *textField) {\n"
1518 " return [self.representedObject solveEquationForTextField:textField];\n"
1519 "}];");
1520}
1521
1522TEST_F(FormatTestObjC, IfNotUnlikely) {
1523 Style = getGoogleStyle(Language: FormatStyle::LK_ObjC);
1524
1525 verifyFormat("if (argc < 5) [obj func:arg];");
1526 verifyFormat("if (argc < 5) [[obj1 method1:arg1] method2:arg2];");
1527 verifyFormat("if (argc < 5) [[foo bar] baz:i[0]];");
1528 verifyFormat("if (argc < 5) [[foo bar] baz:i[0]][1];");
1529
1530 verifyFormat("if (argc < 5)\n"
1531 " [obj func:arg];\n"
1532 "else\n"
1533 " [obj func:arg2];");
1534
1535 verifyFormat("if (argc < 5) [[unlikely]]\n"
1536 " [obj func:arg];\n"
1537 "else [[likely]]\n"
1538 " [obj func:arg2];");
1539}
1540
1541TEST_F(FormatTestObjC, AttributesOnObjCDecl) {
1542 Style.AttributeMacros.push_back(x: "ATTRIBUTE_MACRO");
1543
1544 // Check '__attribute__' macro directly.
1545 verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1546 "@interface Foo\n"
1547 "@end");
1548 verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1549 "@protocol Foo\n"
1550 "@end");
1551 verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1552 "@implementation Foo\n"
1553 "@end");
1554
1555 // Check AttributeMacro gets treated the same, with or without parentheses.
1556 verifyFormat("ATTRIBUTE_MACRO\n"
1557 "@interface Foo\n"
1558 "@end");
1559 verifyFormat("ATTRIBUTE_MACRO(X)\n"
1560 "@interface Foo\n"
1561 "@end");
1562
1563 // Indenter also needs to understand multiple attribute macros.
1564 // Try each of the three kinds paired with each of the other kind.
1565
1566 // Column limit, but no reflow.
1567 verifyFormat("ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO\n"
1568 "@interface Foo\n"
1569 "@end");
1570 verifyFormat("ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X)\n"
1571 "@interface Foo\n"
1572 "@end");
1573 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO\n"
1574 "@interface Foo\n"
1575 "@end");
1576 verifyFormat("ATTRIBUTE_MACRO __attribute__((X))\n"
1577 "@interface Foo\n"
1578 "@end");
1579 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO(X)\n"
1580 "@interface Foo\n"
1581 "@end");
1582 verifyFormat("ATTRIBUTE_MACRO(X) __attribute__((X))\n"
1583 "@interface Foo\n"
1584 "@end");
1585
1586 // Column limit that requires reflow.
1587 Style.ColumnLimit = 30;
1588 verifyFormat("ATTRIBUTE_MACRO(X)\n"
1589 "ATTRIBUTE_MACRO\n"
1590 "@interface Foo\n"
1591 "@end");
1592 verifyFormat("ATTRIBUTE_MACRO\n"
1593 "ATTRIBUTE_MACRO(X)\n"
1594 "@interface Foo\n"
1595 "@end");
1596 verifyFormat("__attribute__((X))\n"
1597 "ATTRIBUTE_MACRO\n"
1598 "@interface Foo\n"
1599 "@end");
1600 verifyFormat("ATTRIBUTE_MACRO\n"
1601 "__attribute__((X))\n"
1602 "@interface Foo\n"
1603 "@end");
1604 verifyFormat("__attribute__((X))\n"
1605 "ATTRIBUTE_MACRO(X)\n"
1606 "@interface Foo\n"
1607 "@end");
1608 verifyFormat("ATTRIBUTE_MACRO(X)\n"
1609 "__attribute__((X))\n"
1610 "@interface Foo\n"
1611 "@end");
1612
1613 // No column limit
1614 Style.ColumnLimit = 0;
1615 verifyFormat("ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO\n"
1616 "@interface Foo\n"
1617 "@end");
1618 verifyFormat("ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X)\n"
1619 "@interface Foo\n"
1620 "@end");
1621 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO\n"
1622 "@interface Foo\n"
1623 "@end");
1624 verifyFormat("ATTRIBUTE_MACRO __attribute__((X))\n"
1625 "@interface Foo\n"
1626 "@end");
1627 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO(X)\n"
1628 "@interface Foo\n"
1629 "@end");
1630 verifyFormat("ATTRIBUTE_MACRO(X) __attribute__((X))\n"
1631 "@interface Foo\n"
1632 "@end");
1633}
1634
1635TEST_F(FormatTestObjC, AttributesOnObjCMethodDecl) {
1636 Style.AttributeMacros.push_back(x: "ATTRIBUTE_MACRO");
1637
1638 // Check '__attribute__' macro directly.
1639 verifyFormat("- (id)init __attribute__((objc_designated_initializer));");
1640
1641 // Check AttributeMacro gets treated the same, with or without parentheses.
1642 verifyFormat("- (id)init ATTRIBUTE_MACRO;");
1643 verifyFormat("- (id)init ATTRIBUTE_MACRO(X);");
1644
1645 // Indenter also needs to understand multiple attribute macros.
1646
1647 // Column limit (default), but no reflow.
1648 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1649 verifyFormat("- (id)init ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1650 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO;");
1651 verifyFormat("- (id)init ATTRIBUTE_MACRO __attribute__((X));");
1652 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO(X);");
1653 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) __attribute__((X));");
1654
1655 // Column limit that requires reflow.
1656 Style.ColumnLimit = 30;
1657
1658 // Reflow after method name.
1659 verifyFormat("- (id)initWithReallyLongName\n"
1660 " __attribute__((X))\n"
1661 " ATTRIBUTE_MACRO;");
1662 verifyFormat("- (id)initWithReallyLongName\n"
1663 " ATTRIBUTE_MACRO(X)\n"
1664 " ATTRIBUTE_MACRO;");
1665 verifyFormat("- (id)initWithReallyLongName\n"
1666 " ATTRIBUTE_MACRO\n"
1667 " ATTRIBUTE_MACRO;");
1668 // Reflow after first macro.
1669 // FIXME: these should indent but don't.
1670#if 0
1671 verifyFormat("- (id)init ATTRIBUTE_MACRO(X)\n"
1672 " ATTRIBUTE_MACRO;");
1673 verifyFormat("- (id)init ATTRIBUTE_MACRO\n"
1674 " ATTRIBUTE_MACRO(X);");
1675 verifyFormat("- (id)init __attribute__((X))\n"
1676 " ATTRIBUTE_MACRO;");
1677 verifyFormat("- (id)init ATTRIBUTE_MACRO\n"
1678 " __attribute__((X));");
1679 verifyFormat("- (id)init __attribute__((X))\n"
1680 " ATTRIBUTE_MACRO(X);");
1681 verifyFormat("- (id)init ATTRIBUTE_MACRO(X)\n"
1682 " __attribute__((X));");
1683#endif
1684
1685 // No column limit.
1686 Style.ColumnLimit = 0;
1687 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1688 verifyFormat("- (id)init ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1689 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO;");
1690 verifyFormat("- (id)init ATTRIBUTE_MACRO __attribute__((X));");
1691 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO(X);");
1692 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) __attribute__((X));");
1693}
1694
1695TEST_F(FormatTestObjC, AttributesOnObjCProperty) {
1696 Style.AttributeMacros.push_back(x: "ATTRIBUTE_MACRO");
1697
1698 // Check '__attribute__' macro directly.
1699 verifyFormat("@property(weak) id delegate "
1700 "__attribute__((objc_designated_initializer));");
1701
1702 // Check AttributeMacro gets treated the same, with or without parentheses.
1703 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO;");
1704 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO(X);");
1705
1706 // Indenter also needs to understand multiple attribute macros.
1707
1708 // Column limit (default), but no reflow.
1709 verifyFormat(
1710 "@property(weak) id delegate ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1711 verifyFormat(
1712 "@property(weak) id delegate ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1713 verifyFormat(
1714 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO;");
1715 verifyFormat(
1716 "@property(weak) id delegate ATTRIBUTE_MACRO __attribute__((X));");
1717 verifyFormat(
1718 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO(X);");
1719 verifyFormat(
1720 "@property(weak) id delegate ATTRIBUTE_MACRO(X) __attribute__((X));");
1721
1722 // Column limit that requires reflow.
1723 Style.ColumnLimit = 50;
1724
1725 // Reflow after method name.
1726 verifyFormat("@property(weak) id delegateWithLongName\n"
1727 " __attribute__((X)) ATTRIBUTE_MACRO;");
1728 verifyFormat("@property(weak) id delegateWithLongName\n"
1729 " ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1730 verifyFormat("@property(weak) id delegateWithLongName\n"
1731 " ATTRIBUTE_MACRO ATTRIBUTE_MACRO;");
1732 // Reflow after first macro.
1733 // FIXME: these should indent but don't.
1734#if 0
1735 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO(X)\n"
1736 " ATTRIBUTE_MACRO;");
1737 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO\n"
1738 " ATTRIBUTE_MACRO(X);");
1739 verifyFormat("@property(weak) id delegate __attribute__((X))\n"
1740 " ATTRIBUTE_MACRO;");
1741 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO\n"
1742 " __attribute__((X));");
1743 verifyFormat("@property(weak) id delegate __attribute__((X))\n"
1744 " ATTRIBUTE_MACRO(X);");
1745 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO(X)\n"
1746 " __attribute__((X));");
1747#endif
1748
1749 // No column limit.
1750 Style.ColumnLimit = 0;
1751 verifyFormat(
1752 "@property(weak) id delegate ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1753 verifyFormat(
1754 "@property(weak) id delegate ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1755 verifyFormat(
1756 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO;");
1757 verifyFormat(
1758 "@property(weak) id delegate ATTRIBUTE_MACRO __attribute__((X));");
1759 verifyFormat(
1760 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO(X);");
1761 verifyFormat(
1762 "@property(weak) id delegate ATTRIBUTE_MACRO(X) __attribute__((X));");
1763}
1764
1765} // end namespace
1766} // namespace test
1767} // end namespace format
1768} // end namespace clang
1769

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of clang/unittests/Format/FormatTestObjC.cpp