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