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