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