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