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