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