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