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 982 TEST_F(FormatTestObjC, ObjCAt) { 983 verifyFormat("@autoreleasepool"); 984 verifyFormat("@catch"); 985 verifyFormat("@class"); 986 verifyFormat("@compatibility_alias"); 987 verifyFormat("@defs"); 988 verifyFormat("@dynamic"); 989 verifyFormat("@encode"); 990 verifyFormat("@end"); 991 verifyFormat("@finally"); 992 verifyFormat("@implementation"); 993 verifyFormat("@import"); 994 verifyFormat("@interface"); 995 verifyFormat("@optional"); 996 verifyFormat("@package"); 997 verifyFormat("@private"); 998 verifyFormat("@property"); 999 verifyFormat("@protected"); 1000 verifyFormat("@protocol"); 1001 verifyFormat("@public"); 1002 verifyFormat("@required"); 1003 verifyFormat("@selector"); 1004 verifyFormat("@synchronized"); 1005 verifyFormat("@synthesize"); 1006 verifyFormat("@throw"); 1007 verifyFormat("@try"); 1008 1009 EXPECT_EQ("@interface", format("@ interface")); 1010 1011 // The precise formatting of this doesn't matter, nobody writes code like 1012 // this. 1013 verifyFormat("@ /*foo*/ interface"); 1014 } 1015 1016 TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) { 1017 verifyFormat("void DoStuffWithBlockType(int (^)(char));"); 1018 verifyFormat("int (^foo)(char, float);"); 1019 verifyFormat("int (^foo[10])(char, float);"); 1020 verifyFormat("int (^foo[kNumEntries])(char, float);"); 1021 verifyFormat("int (^foo[kNumEntries + 10])(char, float);"); 1022 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);"); 1023 } 1024 1025 TEST_F(FormatTestObjC, ObjCSnippets) { 1026 verifyFormat("@autoreleasepool {\n" 1027 " foo();\n" 1028 "}"); 1029 verifyFormat("@class Foo, Bar;"); 1030 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 1031 verifyFormat("@dynamic textColor;"); 1032 verifyFormat("char *buf1 = @encode(int *);"); 1033 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 1034 verifyFormat("char *buf1 = @encode(int **);"); 1035 verifyFormat("Protocol *proto = @protocol(p1);"); 1036 verifyFormat("SEL s = @selector(foo:);"); 1037 verifyFormat("@synchronized(self) {\n" 1038 " f();\n" 1039 "}"); 1040 1041 verifyFormat("@import foo.bar;\n" 1042 "@import baz;"); 1043 1044 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 1045 1046 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 1047 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 1048 1049 verifyFormat("extern UIWindow *MainWindow(void) " 1050 "NS_SWIFT_NAME(getter:MyHelper.mainWindow());"); 1051 1052 verifyFormat("extern UIWindow *MainWindow(void) " 1053 "CF_SWIFT_NAME(getter:MyHelper.mainWindow());"); 1054 1055 Style.ColumnLimit = 50; 1056 verifyFormat("@interface Foo\n" 1057 "- (void)doStuffWithFoo:(id)name\n" 1058 " bar:(id)bar\n" 1059 " baz:(id)baz\n" 1060 " NS_SWIFT_NAME(doStuff(withFoo:bar:baz:));\n" 1061 "@end"); 1062 1063 Style = getMozillaStyle(); 1064 verifyFormat("@property (assign, getter=isEditable) BOOL editable;"); 1065 verifyFormat("@property BOOL editable;"); 1066 1067 Style = getWebKitStyle(); 1068 verifyFormat("@property (assign, getter=isEditable) BOOL editable;"); 1069 verifyFormat("@property BOOL editable;"); 1070 1071 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1072 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 1073 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 1074 } 1075 1076 TEST_F(FormatTestObjC, ObjCForIn) { 1077 verifyFormat("- (void)test {\n" 1078 " for (NSString *n in arrayOfStrings) {\n" 1079 " foo(n);\n" 1080 " }\n" 1081 "}"); 1082 verifyFormat("- (void)test {\n" 1083 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 1084 " foo(n);\n" 1085 " }\n" 1086 "}"); 1087 verifyFormat("for (Foo *x in bar) {\n}"); 1088 verifyFormat("for (Foo *x in [bar baz]) {\n}"); 1089 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}"); 1090 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}"); 1091 verifyFormat("for (Foo *x in [bar baz:^{\n" 1092 " [uh oh];\n" 1093 " }]) {\n}"); 1094 } 1095 1096 TEST_F(FormatTestObjC, ObjCCxxKeywords) { 1097 verifyFormat("+ (instancetype)new {\n" 1098 " return nil;\n" 1099 "}\n"); 1100 verifyFormat("+ (instancetype)myNew {\n" 1101 " return [self new];\n" 1102 "}\n"); 1103 verifyFormat("SEL NewSelector(void) { return @selector(new); }\n"); 1104 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n"); 1105 verifyFormat("+ (instancetype)delete {\n" 1106 " return nil;\n" 1107 "}\n"); 1108 verifyFormat("+ (instancetype)myDelete {\n" 1109 " return [self delete];\n" 1110 "}\n"); 1111 verifyFormat("SEL DeleteSelector(void) { return @selector(delete); }\n"); 1112 verifyFormat("SEL MacroSelector(void) { return MACRO(delete); }\n"); 1113 verifyFormat("MACRO(new:)\n"); 1114 verifyFormat("MACRO(delete:)\n"); 1115 verifyFormat("foo = @{MACRO(new:) : MACRO(delete:)}\n"); 1116 verifyFormat("@implementation Foo\n" 1117 "// Testing\n" 1118 "- (Class)class {\n" 1119 "}\n" 1120 "- (void)foo {\n" 1121 "}\n" 1122 "@end\n"); 1123 verifyFormat("@implementation Foo\n" 1124 "- (Class)class {\n" 1125 "}\n" 1126 "- (void)foo {\n" 1127 "}\n" 1128 "@end"); 1129 verifyFormat("@implementation Foo\n" 1130 "+ (Class)class {\n" 1131 "}\n" 1132 "- (void)foo {\n" 1133 "}\n" 1134 "@end"); 1135 verifyFormat("@implementation Foo\n" 1136 "- (Class)class:(Class)klass {\n" 1137 "}\n" 1138 "- (void)foo {\n" 1139 "}\n" 1140 "@end"); 1141 verifyFormat("@implementation Foo\n" 1142 "+ (Class)class:(Class)klass {\n" 1143 "}\n" 1144 "- (void)foo {\n" 1145 "}\n" 1146 "@end"); 1147 1148 verifyFormat("@interface Foo\n" 1149 "// Testing\n" 1150 "- (Class)class;\n" 1151 "- (void)foo;\n" 1152 "@end\n"); 1153 verifyFormat("@interface Foo\n" 1154 "- (Class)class;\n" 1155 "- (void)foo;\n" 1156 "@end"); 1157 verifyFormat("@interface Foo\n" 1158 "+ (Class)class;\n" 1159 "- (void)foo;\n" 1160 "@end"); 1161 verifyFormat("@interface Foo\n" 1162 "- (Class)class:(Class)klass;\n" 1163 "- (void)foo;\n" 1164 "@end"); 1165 verifyFormat("@interface Foo\n" 1166 "+ (Class)class:(Class)klass;\n" 1167 "- (void)foo;\n" 1168 "@end"); 1169 } 1170 1171 TEST_F(FormatTestObjC, ObjCLiterals) { 1172 verifyFormat("@\"String\""); 1173 verifyFormat("@1"); 1174 verifyFormat("@+4.8"); 1175 verifyFormat("@-4"); 1176 verifyFormat("@1LL"); 1177 verifyFormat("@.5"); 1178 verifyFormat("@'c'"); 1179 verifyFormat("@true"); 1180 1181 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 1182 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 1183 verifyFormat("NSNumber *favoriteColor = @(Green);"); 1184 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 1185 1186 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 1187 } 1188 1189 TEST_F(FormatTestObjC, ObjCDictLiterals) { 1190 verifyFormat("@{"); 1191 verifyFormat("@{}"); 1192 verifyFormat("@{@\"one\" : @1}"); 1193 verifyFormat("return @{@\"one\" : @1;"); 1194 verifyFormat("@{@\"one\" : @1}"); 1195 1196 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 1197 verifyFormat("@{\n" 1198 " @\"one\" : @{@2 : @1},\n" 1199 "}"); 1200 1201 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 1202 verifyIncompleteFormat("[self setDict:@{}"); 1203 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 1204 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 1205 verifyFormat( 1206 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 1207 verifyFormat( 1208 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 1209 1210 verifyFormat("NSDictionary *d = @{\n" 1211 " @\"nam\" : NSUserNam(),\n" 1212 " @\"dte\" : [NSDate date],\n" 1213 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 1214 "};"); 1215 verifyFormat( 1216 "@{\n" 1217 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 1218 "regularFont,\n" 1219 "};"); 1220 verifyFormat( 1221 "@{\n" 1222 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 1223 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 1224 "};"); 1225 1226 // We should try to be robust in case someone forgets the "@". 1227 verifyFormat("NSDictionary *d = {\n" 1228 " @\"nam\" : NSUserNam(),\n" 1229 " @\"dte\" : [NSDate date],\n" 1230 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 1231 "};"); 1232 verifyFormat("NSMutableDictionary *dictionary =\n" 1233 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 1234 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 1235 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 1236 " cccccccccccccccc : ccccccccccccccc\n" 1237 " }];"); 1238 1239 // Ensure that casts before the key are kept on the same line as the key. 1240 verifyFormat( 1241 "NSDictionary *d = @{\n" 1242 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1243 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 1244 "};"); 1245 Style.ColumnLimit = 40; 1246 verifyFormat("int Foo() {\n" 1247 " a12345 = @{a12345 : a12345};\n" 1248 "}"); 1249 verifyFormat("int Foo() {\n" 1250 " a12345 = @{a12345 : @(a12345)};\n" 1251 "}"); 1252 verifyFormat("int Foo() {\n" 1253 " a12345 = @{(Foo *)a12345 : @(a12345)};\n" 1254 "}"); 1255 verifyFormat("int Foo() {\n" 1256 " a12345 = @{@(a12345) : a12345};\n" 1257 "}"); 1258 verifyFormat("int Foo() {\n" 1259 " a12345 = @{@(a12345) : @YES};\n" 1260 "}"); 1261 Style.SpacesInContainerLiterals = false; 1262 verifyFormat("int Foo() {\n" 1263 " b12345 = @{b12345: b12345};\n" 1264 "}"); 1265 verifyFormat("int Foo() {\n" 1266 " b12345 = @{(Foo *)b12345: @(b12345)};\n" 1267 "}"); 1268 Style.SpacesInContainerLiterals = true; 1269 1270 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1271 verifyFormat( 1272 "@{\n" 1273 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 1274 "regularFont,\n" 1275 "};"); 1276 } 1277 1278 TEST_F(FormatTestObjC, ObjCArrayLiterals) { 1279 verifyIncompleteFormat("@["); 1280 verifyFormat("@[]"); 1281 verifyFormat( 1282 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 1283 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 1284 verifyFormat("NSArray *array = @[ [foo description] ];"); 1285 1286 verifyFormat( 1287 "NSArray *some_variable = @[\n" 1288 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 1289 " @\"aaaaaaaaaaaaaaaaa\",\n" 1290 " @\"aaaaaaaaaaaaaaaaa\",\n" 1291 " @\"aaaaaaaaaaaaaaaaa\",\n" 1292 "];"); 1293 verifyFormat( 1294 "NSArray *some_variable = @[\n" 1295 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 1296 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 1297 "];"); 1298 verifyFormat("NSArray *some_variable = @[\n" 1299 " @\"aaaaaaaaaaaaaaaaa\",\n" 1300 " @\"aaaaaaaaaaaaaaaaa\",\n" 1301 " @\"aaaaaaaaaaaaaaaaa\",\n" 1302 " @\"aaaaaaaaaaaaaaaaa\",\n" 1303 "];"); 1304 verifyFormat("NSArray *array = @[\n" 1305 " @\"a\",\n" 1306 " @\"a\",\n" // Trailing comma -> one per line. 1307 "];"); 1308 1309 // We should try to be robust in case someone forgets the "@". 1310 verifyFormat("NSArray *some_variable = [\n" 1311 " @\"aaaaaaaaaaaaaaaaa\",\n" 1312 " @\"aaaaaaaaaaaaaaaaa\",\n" 1313 " @\"aaaaaaaaaaaaaaaaa\",\n" 1314 " @\"aaaaaaaaaaaaaaaaa\",\n" 1315 "];"); 1316 verifyFormat( 1317 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 1318 " index:(NSUInteger)index\n" 1319 " nonDigitAttributes:\n" 1320 " (NSDictionary *)noDigitAttributes;"); 1321 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 1322 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 1323 "]];"); 1324 Style.ColumnLimit = 40; 1325 verifyFormat("int Foo() {\n" 1326 " a12345 = @[ a12345, a12345 ];\n" 1327 "}"); 1328 verifyFormat("int Foo() {\n" 1329 " a123 = @[ (Foo *)a12345, @(a12345) ];\n" 1330 "}"); 1331 Style.SpacesInContainerLiterals = false; 1332 verifyFormat("int Foo() {\n" 1333 " b12345 = @[b12345, b12345];\n" 1334 "}"); 1335 verifyFormat("int Foo() {\n" 1336 " b12345 = @[(Foo *)b12345, @(b12345)];\n" 1337 "}"); 1338 Style.SpacesInContainerLiterals = true; 1339 Style.ColumnLimit = 20; 1340 // We can't break string literals inside NSArray literals 1341 // (that raises -Wobjc-string-concatenation). 1342 verifyFormat("NSArray *foo = @[\n" 1343 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1344 "];\n"); 1345 } 1346 1347 TEST_F(FormatTestObjC, BreaksCallStatementWhereSemiJustOverTheLimit) { 1348 Style.ColumnLimit = 60; 1349 // If the statement starting with 'a = ...' is put on a single line, the ';' 1350 // is at line 61. 1351 verifyFormat("int f(int a) {\n" 1352 " a = [self aaaaaaaaaa:bbbbbbbbb\n" 1353 " ccccccccc:dddddddd\n" 1354 " ee:fddd];\n" 1355 "}"); 1356 } 1357 1358 TEST_F(FormatTestObjC, AlwaysBreakBeforeMultilineStrings) { 1359 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1360 Style.ColumnLimit = 40; 1361 verifyFormat("aaaa = @\"bbbb\"\n" 1362 " @\"cccc\";"); 1363 verifyFormat("aaaa(@\"bbbb\"\n" 1364 " @\"cccc\");"); 1365 verifyFormat("aaaa(qqq, @\"bbbb\"\n" 1366 " @\"cccc\");"); 1367 verifyFormat("[aaaa qqqq:@\"bbbb\"\n" 1368 " @\"cccc\"];"); 1369 verifyFormat("aaaa = [aaaa qqqq:@\"bbbb\"\n" 1370 " @\"cccc\"];"); 1371 verifyFormat("[aaaa qqqq:@\"bbbb\"\n" 1372 " @\"cccc\"\n" 1373 " rr:42\n" 1374 " ssssss:@\"ee\"\n" 1375 " @\"fffff\"];"); 1376 } 1377 1378 TEST_F(FormatTestObjC, DisambiguatesCallsFromCppLambdas) { 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');"); 1388 verifyFormat("x = ([a foo:bar] && b->c == 'd');"); 1389 verifyFormat("x = ([a foo:bar] == b->c == 'd');"); 1390 verifyFormat("x = ([a foo:bar] != b->c == 'd');"); 1391 verifyFormat("x = ([a foo:bar] <= b->c == 'd');"); 1392 verifyFormat("x = ([a foo:bar] >= b->c == 'd');"); 1393 verifyFormat("x = ([a foo:bar] << b->c == 'd');"); 1394 verifyFormat("x = ([a foo:bar] ? b->c == 'd' : 'e');"); 1395 // FIXME: The following are wrongly classified as C++ lambda expressions. 1396 // For example this code: 1397 // x = ([a foo:bar] & b->c == 'd'); 1398 // is formatted as: 1399 // 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 } 1405 1406 TEST_F(FormatTestObjC, DisambiguatesCallsFromStructuredBindings) { 1407 verifyFormat("int f() {\n" 1408 " if (a && [f arg])\n" 1409 " return 0;\n" 1410 "}"); 1411 verifyFormat("int f() {\n" 1412 " if (a & [f arg])\n" 1413 " return 0;\n" 1414 "}"); 1415 verifyFormat("int f() {\n" 1416 " for (auto &[elem] : list)\n" 1417 " return 0;\n" 1418 "}"); 1419 verifyFormat("int f() {\n" 1420 " for (auto &&[elem] : list)\n" 1421 " return 0;\n" 1422 "}"); 1423 verifyFormat( 1424 "int f() {\n" 1425 " for (auto /**/ const /**/ volatile /**/ && /**/ [elem] : list)\n" 1426 " return 0;\n" 1427 "}"); 1428 } 1429 1430 TEST_F(FormatTestObjC, BreakLineBeforeNestedBlockParam) { 1431 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1432 Style.ObjCBreakBeforeNestedBlockParam = false; 1433 Style.ColumnLimit = 0; 1434 1435 verifyFormat("[self.test1 t:self callback:^(typeof(self) self, NSNumber *u, " 1436 "NSNumber *v) {\n" 1437 " u = v;\n" 1438 "}]"); 1439 1440 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, " 1441 "NSNumber *u, NSNumber *v) {\n" 1442 " u = v;\n" 1443 "}]"); 1444 1445 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, " 1446 "NSNumber *u, NSNumber *v) {\n" 1447 " u = c;\n" 1448 "} w:self callback2:^(typeof(self) self, NSNumber *a, NSNumber " 1449 "*b, NSNumber *c) {\n" 1450 " b = c;\n" 1451 "}]"); 1452 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, " 1453 "NSNumber *u, NSNumber *v) {\n" 1454 " u = v;\n" 1455 "} z:self]"); 1456 1457 Style.ColumnLimit = 80; 1458 verifyFormat( 1459 "[self.test_method a:self b:self\n" 1460 " callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {\n" 1461 " u = v;\n" 1462 " }]"); 1463 1464 verifyFormat("[self block:^(void) {\n" 1465 " doStuff();\n" 1466 "} completionHandler:^(void) {\n" 1467 " doStuff();\n" 1468 " [self block:^(void) {\n" 1469 " doStuff();\n" 1470 " } completionHandler:^(void) {\n" 1471 " doStuff();\n" 1472 " }];\n" 1473 "}];"); 1474 1475 Style.ColumnLimit = 0; 1476 verifyFormat("[[SessionService sharedService] " 1477 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 1478 " if (window) {\n" 1479 " [self windowDidLoad:window];\n" 1480 " } else {\n" 1481 " [self errorLoadingWindow];\n" 1482 " }\n" 1483 "}];"); 1484 verifyFormat("[controller test:^{\n" 1485 " doStuff();\n" 1486 "} withTimeout:5 completionHandler:^{\n" 1487 " doStuff();\n" 1488 "}];"); 1489 verifyFormat( 1490 "[self setupTextFieldSignals:@[\n" 1491 " self.documentWidthField,\n" 1492 " self.documentHeightField,\n" 1493 "] solver:^(NSTextField *textField) {\n" 1494 " return [self.representedObject solveEquationForTextField:textField];\n" 1495 "}];"); 1496 } 1497 1498 TEST_F(FormatTestObjC, IfNotUnlikely) { 1499 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1500 1501 verifyFormat("if (argc < 5) [obj func:arg];"); 1502 verifyFormat("if (argc < 5) [[obj1 method1:arg1] method2:arg2];"); 1503 verifyFormat("if (argc < 5) [[foo bar] baz:i[0]];"); 1504 verifyFormat("if (argc < 5) [[foo bar] baz:i[0]][1];"); 1505 1506 verifyFormat("if (argc < 5)\n" 1507 " [obj func:arg];\n" 1508 "else\n" 1509 " [obj func:arg2];"); 1510 1511 verifyFormat("if (argc < 5) [[unlikely]]\n" 1512 " [obj func:arg];\n" 1513 "else [[likely]]\n" 1514 " [obj func:arg2];"); 1515 } 1516 1517 } // end namespace 1518 } // end namespace format 1519 } // end namespace clang 1520