FormatTestObjC.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. //===- unittest/Format/FormatTestObjC.cpp - Formatting unit tests----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Format/Format.h"
  10. #include "../Tooling/ReplacementTest.h"
  11. #include "FormatTestUtils.h"
  12. #include "clang/Frontend/TextDiagnosticPrinter.h"
  13. #include "llvm/Support/Debug.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include "gtest/gtest.h"
  16. #define DEBUG_TYPE "format-test"
  17. using clang::tooling::ReplacementTest;
  18. namespace clang {
  19. namespace format {
  20. namespace {
  21. class FormatTestObjC : public ::testing::Test {
  22. protected:
  23. FormatTestObjC() {
  24. Style = getLLVMStyle();
  25. Style.Language = FormatStyle::LK_ObjC;
  26. }
  27. enum StatusCheck {
  28. SC_ExpectComplete,
  29. SC_ExpectIncomplete,
  30. SC_DoNotCheck
  31. };
  32. std::string format(llvm::StringRef Code,
  33. StatusCheck CheckComplete = SC_ExpectComplete) {
  34. DEBUG(llvm::errs() << "---\n");
  35. DEBUG(llvm::errs() << Code << "\n\n");
  36. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  37. FormattingAttemptStatus Status;
  38. tooling::Replacements Replaces =
  39. reformat(Style, Code, Ranges, "<stdin>", &Status);
  40. if (CheckComplete != SC_DoNotCheck) {
  41. bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
  42. EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
  43. << Code << "\n\n";
  44. }
  45. auto Result = applyAllReplacements(Code, Replaces);
  46. EXPECT_TRUE(static_cast<bool>(Result));
  47. DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
  48. return *Result;
  49. }
  50. void verifyFormat(StringRef Code) {
  51. EXPECT_EQ(Code.str(), format(test::messUp(Code)));
  52. }
  53. void verifyIncompleteFormat(StringRef Code) {
  54. EXPECT_EQ(Code.str(), format(test::messUp(Code), SC_ExpectIncomplete));
  55. }
  56. FormatStyle Style;
  57. };
  58. TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
  59. auto Style = getStyle("LLVM", "a.h", "none", "@interface\n"
  60. "- (id)init;");
  61. ASSERT_TRUE((bool)Style);
  62. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  63. Style = getStyle("LLVM", "a.h", "none", "@interface\n"
  64. "+ (id)init;");
  65. ASSERT_TRUE((bool)Style);
  66. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  67. // No recognizable ObjC.
  68. Style = getStyle("LLVM", "a.h", "none", "void f() {}");
  69. ASSERT_TRUE((bool)Style);
  70. EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
  71. }
  72. TEST_F(FormatTestObjC, FormatObjCTryCatch) {
  73. verifyFormat("@try {\n"
  74. " f();\n"
  75. "} @catch (NSException e) {\n"
  76. " @throw;\n"
  77. "} @finally {\n"
  78. " exit(42);\n"
  79. "}");
  80. verifyFormat("DEBUG({\n"
  81. " @try {\n"
  82. " } @finally {\n"
  83. " }\n"
  84. "});\n");
  85. }
  86. TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
  87. verifyFormat("@autoreleasepool {\n"
  88. " f();\n"
  89. "}\n"
  90. "@autoreleasepool {\n"
  91. " f();\n"
  92. "}\n");
  93. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  94. verifyFormat("@autoreleasepool\n"
  95. "{\n"
  96. " f();\n"
  97. "}\n"
  98. "@autoreleasepool\n"
  99. "{\n"
  100. " f();\n"
  101. "}\n");
  102. }
  103. TEST_F(FormatTestObjC, FormatObjCInterface) {
  104. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  105. "@public\n"
  106. " int field1;\n"
  107. "@protected\n"
  108. " int field2;\n"
  109. "@private\n"
  110. " int field3;\n"
  111. "@package\n"
  112. " int field4;\n"
  113. "}\n"
  114. "+ (id)init;\n"
  115. "@end");
  116. verifyFormat("@interface /* wait for it */ Foo\n"
  117. "+ (id)init;\n"
  118. "// Look, a comment!\n"
  119. "- (int)answerWith:(int)i;\n"
  120. "@end");
  121. verifyFormat("@interface Foo\n"
  122. "@end\n"
  123. "@interface Bar\n"
  124. "@end");
  125. verifyFormat("@interface Foo : Bar\n"
  126. "+ (id)init;\n"
  127. "@end");
  128. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  129. "+ (id)init;\n"
  130. "@end");
  131. verifyFormat("@interface Foo (HackStuff)\n"
  132. "+ (id)init;\n"
  133. "@end");
  134. verifyFormat("@interface Foo ()\n"
  135. "+ (id)init;\n"
  136. "@end");
  137. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  138. "+ (id)init;\n"
  139. "@end");
  140. verifyFormat("@interface Foo {\n"
  141. " int _i;\n"
  142. "}\n"
  143. "+ (id)init;\n"
  144. "@end");
  145. verifyFormat("@interface Foo : Bar {\n"
  146. " int _i;\n"
  147. "}\n"
  148. "+ (id)init;\n"
  149. "@end");
  150. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  151. " int _i;\n"
  152. "}\n"
  153. "+ (id)init;\n"
  154. "@end");
  155. verifyFormat("@interface Foo (HackStuff) {\n"
  156. " int _i;\n"
  157. "}\n"
  158. "+ (id)init;\n"
  159. "@end");
  160. verifyFormat("@interface Foo () {\n"
  161. " int _i;\n"
  162. "}\n"
  163. "+ (id)init;\n"
  164. "@end");
  165. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  166. " int _i;\n"
  167. "}\n"
  168. "+ (id)init;\n"
  169. "@end");
  170. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  171. verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  172. " @public\n"
  173. " int field1;\n"
  174. " @protected\n"
  175. " int field2;\n"
  176. " @private\n"
  177. " int field3;\n"
  178. " @package\n"
  179. " int field4;\n"
  180. "}\n"
  181. "+ (id)init;\n"
  182. "@end");
  183. verifyFormat("@interface Foo : Bar<Baz, Quux>\n"
  184. "+ (id)init;\n"
  185. "@end");
  186. verifyFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  187. "+ (id)init;\n"
  188. "@end");
  189. Style.BinPackParameters = false;
  190. Style.ColumnLimit = 80;
  191. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
  192. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  193. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  194. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  195. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  196. "}");
  197. }
  198. TEST_F(FormatTestObjC, FormatObjCImplementation) {
  199. verifyFormat("@implementation Foo : NSObject {\n"
  200. "@public\n"
  201. " int field1;\n"
  202. "@protected\n"
  203. " int field2;\n"
  204. "@private\n"
  205. " int field3;\n"
  206. "@package\n"
  207. " int field4;\n"
  208. "}\n"
  209. "+ (id)init {\n}\n"
  210. "@end");
  211. verifyFormat("@implementation Foo\n"
  212. "+ (id)init {\n"
  213. " if (true)\n"
  214. " return nil;\n"
  215. "}\n"
  216. "// Look, a comment!\n"
  217. "- (int)answerWith:(int)i {\n"
  218. " return i;\n"
  219. "}\n"
  220. "+ (int)answerWith:(int)i {\n"
  221. " return i;\n"
  222. "}\n"
  223. "@end");
  224. verifyFormat("@implementation Foo\n"
  225. "@end\n"
  226. "@implementation Bar\n"
  227. "@end");
  228. EXPECT_EQ("@implementation Foo : Bar\n"
  229. "+ (id)init {\n}\n"
  230. "- (void)foo {\n}\n"
  231. "@end",
  232. format("@implementation Foo : Bar\n"
  233. "+(id)init{}\n"
  234. "-(void)foo{}\n"
  235. "@end"));
  236. verifyFormat("@implementation Foo {\n"
  237. " int _i;\n"
  238. "}\n"
  239. "+ (id)init {\n}\n"
  240. "@end");
  241. verifyFormat("@implementation Foo : Bar {\n"
  242. " int _i;\n"
  243. "}\n"
  244. "+ (id)init {\n}\n"
  245. "@end");
  246. verifyFormat("@implementation Foo (HackStuff)\n"
  247. "+ (id)init {\n}\n"
  248. "@end");
  249. verifyFormat("@implementation ObjcClass\n"
  250. "- (void)method;\n"
  251. "{}\n"
  252. "@end");
  253. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  254. verifyFormat("@implementation Foo : NSObject {\n"
  255. " @public\n"
  256. " int field1;\n"
  257. " @protected\n"
  258. " int field2;\n"
  259. " @private\n"
  260. " int field3;\n"
  261. " @package\n"
  262. " int field4;\n"
  263. "}\n"
  264. "+ (id)init {\n}\n"
  265. "@end");
  266. }
  267. TEST_F(FormatTestObjC, FormatObjCProtocol) {
  268. verifyFormat("@protocol Foo\n"
  269. "@property(weak) id delegate;\n"
  270. "- (NSUInteger)numberOfThings;\n"
  271. "@end");
  272. verifyFormat("@protocol MyProtocol <NSObject>\n"
  273. "- (NSUInteger)numberOfThings;\n"
  274. "@end");
  275. verifyFormat("@protocol Foo;\n"
  276. "@protocol Bar;\n");
  277. verifyFormat("@protocol Foo\n"
  278. "@end\n"
  279. "@protocol Bar\n"
  280. "@end");
  281. verifyFormat("@protocol myProtocol\n"
  282. "- (void)mandatoryWithInt:(int)i;\n"
  283. "@optional\n"
  284. "- (void)optional;\n"
  285. "@required\n"
  286. "- (void)required;\n"
  287. "@optional\n"
  288. "@property(assign) int madProp;\n"
  289. "@end\n");
  290. verifyFormat("@property(nonatomic, assign, readonly)\n"
  291. " int *looooooooooooooooooooooooooooongNumber;\n"
  292. "@property(nonatomic, assign, readonly)\n"
  293. " NSString *looooooooooooooooooooooooooooongName;");
  294. verifyFormat("@implementation PR18406\n"
  295. "}\n"
  296. "@end");
  297. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  298. verifyFormat("@protocol MyProtocol<NSObject>\n"
  299. "- (NSUInteger)numberOfThings;\n"
  300. "@end");
  301. }
  302. TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
  303. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  304. " rect:(NSRect)theRect\n"
  305. " interval:(float)theInterval {\n"
  306. "}");
  307. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  308. " longKeyword:(NSRect)theRect\n"
  309. " longerKeyword:(float)theInterval\n"
  310. " error:(NSError **)theError {\n"
  311. "}");
  312. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  313. " longKeyword:(NSRect)theRect\n"
  314. " evenLongerKeyword:(float)theInterval\n"
  315. " error:(NSError **)theError {\n"
  316. "}");
  317. Style.ColumnLimit = 60;
  318. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  319. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  320. " NS_DESIGNATED_INITIALIZER;");
  321. verifyFormat("- (void)drawRectOn:(id)surface\n"
  322. " ofSize:(size_t)height\n"
  323. " :(size_t)width;");
  324. // Continuation indent width should win over aligning colons if the function
  325. // name is long.
  326. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  327. Style.ColumnLimit = 40;
  328. Style.IndentWrappedFunctionNames = true;
  329. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  330. " dontAlignNamef:(NSRect)theRect {\n"
  331. "}");
  332. // Make sure we don't break aligning for short parameter names.
  333. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  334. " aShortf:(NSRect)theRect {\n"
  335. "}");
  336. // Format pairs correctly.
  337. Style.ColumnLimit = 80;
  338. verifyFormat("- (void)drawRectOn:(id)surface\n"
  339. " ofSize:(aaaaaaaa)height\n"
  340. " :(size_t)width\n"
  341. " atOrigin:(size_t)x\n"
  342. " :(size_t)y\n"
  343. " aaaaa:(a)yyy\n"
  344. " bbb:(d)cccc;");
  345. verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
  346. }
  347. TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
  348. verifyFormat("[foo bar:baz];");
  349. verifyFormat("return [foo bar:baz];");
  350. verifyFormat("return (a)[foo bar:baz];");
  351. verifyFormat("f([foo bar:baz]);");
  352. verifyFormat("f(2, [foo bar:baz]);");
  353. verifyFormat("f(2, a ? b : c);");
  354. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  355. // Unary operators.
  356. verifyFormat("int a = +[foo bar:baz];");
  357. verifyFormat("int a = -[foo bar:baz];");
  358. verifyFormat("int a = ![foo bar:baz];");
  359. verifyFormat("int a = ~[foo bar:baz];");
  360. verifyFormat("int a = ++[foo bar:baz];");
  361. verifyFormat("int a = --[foo bar:baz];");
  362. verifyFormat("int a = sizeof [foo bar:baz];");
  363. verifyFormat("int a = alignof [foo bar:baz];");
  364. verifyFormat("int a = &[foo bar:baz];");
  365. verifyFormat("int a = *[foo bar:baz];");
  366. // FIXME: Make casts work, without breaking f()[4].
  367. // verifyFormat("int a = (int)[foo bar:baz];");
  368. // verifyFormat("return (int)[foo bar:baz];");
  369. // verifyFormat("(void)[foo bar:baz];");
  370. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  371. // Binary operators.
  372. verifyFormat("[foo bar:baz], [foo bar:baz];");
  373. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  374. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  375. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  376. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  377. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  378. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  379. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  380. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  381. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  382. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  383. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  384. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  385. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  386. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  387. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  388. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  389. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  390. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  391. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  392. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  393. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  394. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  395. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  396. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  397. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  398. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  399. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  400. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  401. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  402. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  403. // Whew!
  404. verifyFormat("return in[42];");
  405. verifyFormat("for (auto v : in[1]) {\n}");
  406. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  407. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  408. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  409. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  410. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  411. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  412. "}");
  413. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  414. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  415. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  416. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  417. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  418. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  419. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  420. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  421. verifyFormat("[button setAction:@selector(zoomOut:)];");
  422. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  423. verifyFormat("arr[[self indexForFoo:a]];");
  424. verifyFormat("throw [self errorFor:a];");
  425. verifyFormat("@throw [self errorFor:a];");
  426. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  427. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  428. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  429. // This tests that the formatter doesn't break after "backing" but before ":",
  430. // which would be at 80 columns.
  431. verifyFormat(
  432. "void f() {\n"
  433. " if ((self = [super initWithContentRect:contentRect\n"
  434. " styleMask:styleMask ?: otherMask\n"
  435. " backing:NSBackingStoreBuffered\n"
  436. " defer:YES]))");
  437. verifyFormat(
  438. "[foo checkThatBreakingAfterColonWorksOk:\n"
  439. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  440. verifyFormat("[myObj short:arg1 // Force line break\n"
  441. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  442. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  443. " error:arg4];");
  444. verifyFormat(
  445. "void f() {\n"
  446. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  447. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  448. " pos.width(), pos.height())\n"
  449. " styleMask:NSBorderlessWindowMask\n"
  450. " backing:NSBackingStoreBuffered\n"
  451. " defer:NO]);\n"
  452. "}");
  453. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  454. " with:contentsNativeView];");
  455. verifyFormat(
  456. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  457. " owner:nillllll];");
  458. verifyFormat(
  459. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  460. " forType:kBookmarkButtonDragType];");
  461. verifyFormat("[defaultCenter addObserver:self\n"
  462. " selector:@selector(willEnterFullscreen)\n"
  463. " name:kWillEnterFullscreenNotification\n"
  464. " object:nil];");
  465. verifyFormat("[image_rep drawInRect:drawRect\n"
  466. " fromRect:NSZeroRect\n"
  467. " operation:NSCompositeCopy\n"
  468. " fraction:1.0\n"
  469. " respectFlipped:NO\n"
  470. " hints:nil];");
  471. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  472. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  473. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  474. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  475. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  476. " aaaaaaaaaaaaaaaaaaaaaa];");
  477. verifyFormat(
  478. "scoped_nsobject<NSTextField> message(\n"
  479. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  480. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  481. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  482. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  483. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  484. " aaaa:bbb];");
  485. verifyFormat("[self param:function( //\n"
  486. " parameter)]");
  487. verifyFormat(
  488. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  489. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  490. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  491. // Variadic parameters.
  492. verifyFormat(
  493. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  494. verifyFormat(
  495. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  496. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  497. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  498. verifyFormat("[self // break\n"
  499. " a:a\n"
  500. " aaa:aaa];");
  501. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  502. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  503. // Formats pair-parameters.
  504. verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
  505. verifyFormat("[I drawRectOn:surface //\n"
  506. " ofSize:aa:bbb\n"
  507. " atOrigin:cc:dd];");
  508. Style.ColumnLimit = 70;
  509. verifyFormat(
  510. "void f() {\n"
  511. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  512. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  513. " pos.width(), pos.height())\n"
  514. " syeMask:NSBorderlessWindowMask\n"
  515. " bking:NSBackingStoreBuffered\n"
  516. " der:NO]);\n"
  517. "}");
  518. Style.ColumnLimit = 60;
  519. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  520. " .aaaaaaaa];"); // FIXME: Indentation seems off.
  521. // FIXME: This violates the column limit.
  522. verifyFormat(
  523. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  524. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  525. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  526. Style = getChromiumStyle(FormatStyle::LK_ObjC);
  527. Style.ColumnLimit = 80;
  528. verifyFormat(
  529. "void f() {\n"
  530. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  531. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  532. " pos.width(), pos.height())\n"
  533. " styleMask:NSBorderlessWindowMask\n"
  534. " backing:NSBackingStoreBuffered\n"
  535. " defer:NO]);\n"
  536. "}");
  537. }
  538. TEST_F(FormatTestObjC, ObjCAt) {
  539. verifyFormat("@autoreleasepool");
  540. verifyFormat("@catch");
  541. verifyFormat("@class");
  542. verifyFormat("@compatibility_alias");
  543. verifyFormat("@defs");
  544. verifyFormat("@dynamic");
  545. verifyFormat("@encode");
  546. verifyFormat("@end");
  547. verifyFormat("@finally");
  548. verifyFormat("@implementation");
  549. verifyFormat("@import");
  550. verifyFormat("@interface");
  551. verifyFormat("@optional");
  552. verifyFormat("@package");
  553. verifyFormat("@private");
  554. verifyFormat("@property");
  555. verifyFormat("@protected");
  556. verifyFormat("@protocol");
  557. verifyFormat("@public");
  558. verifyFormat("@required");
  559. verifyFormat("@selector");
  560. verifyFormat("@synchronized");
  561. verifyFormat("@synthesize");
  562. verifyFormat("@throw");
  563. verifyFormat("@try");
  564. EXPECT_EQ("@interface", format("@ interface"));
  565. // The precise formatting of this doesn't matter, nobody writes code like
  566. // this.
  567. verifyFormat("@ /*foo*/ interface");
  568. }
  569. TEST_F(FormatTestObjC, ObjCSnippets) {
  570. verifyFormat("@autoreleasepool {\n"
  571. " foo();\n"
  572. "}");
  573. verifyFormat("@class Foo, Bar;");
  574. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  575. verifyFormat("@dynamic textColor;");
  576. verifyFormat("char *buf1 = @encode(int *);");
  577. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  578. verifyFormat("char *buf1 = @encode(int **);");
  579. verifyFormat("Protocol *proto = @protocol(p1);");
  580. verifyFormat("SEL s = @selector(foo:);");
  581. verifyFormat("@synchronized(self) {\n"
  582. " f();\n"
  583. "}");
  584. verifyFormat("@import foo.bar;\n"
  585. "@import baz;");
  586. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  587. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  588. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  589. Style = getMozillaStyle();
  590. verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
  591. verifyFormat("@property BOOL editable;");
  592. Style = getWebKitStyle();
  593. verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
  594. verifyFormat("@property BOOL editable;");
  595. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  596. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  597. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  598. }
  599. TEST_F(FormatTestObjC, ObjCForIn) {
  600. verifyFormat("- (void)test {\n"
  601. " for (NSString *n in arrayOfStrings) {\n"
  602. " foo(n);\n"
  603. " }\n"
  604. "}");
  605. verifyFormat("- (void)test {\n"
  606. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  607. " foo(n);\n"
  608. " }\n"
  609. "}");
  610. }
  611. TEST_F(FormatTestObjC, ObjCLiterals) {
  612. verifyFormat("@\"String\"");
  613. verifyFormat("@1");
  614. verifyFormat("@+4.8");
  615. verifyFormat("@-4");
  616. verifyFormat("@1LL");
  617. verifyFormat("@.5");
  618. verifyFormat("@'c'");
  619. verifyFormat("@true");
  620. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  621. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  622. verifyFormat("NSNumber *favoriteColor = @(Green);");
  623. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  624. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  625. }
  626. TEST_F(FormatTestObjC, ObjCDictLiterals) {
  627. verifyFormat("@{");
  628. verifyFormat("@{}");
  629. verifyFormat("@{@\"one\" : @1}");
  630. verifyFormat("return @{@\"one\" : @1;");
  631. verifyFormat("@{@\"one\" : @1}");
  632. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  633. verifyFormat("@{\n"
  634. " @\"one\" : @{@2 : @1},\n"
  635. "}");
  636. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  637. verifyIncompleteFormat("[self setDict:@{}");
  638. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  639. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  640. verifyFormat(
  641. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  642. verifyFormat(
  643. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  644. verifyFormat("NSDictionary *d = @{\n"
  645. " @\"nam\" : NSUserNam(),\n"
  646. " @\"dte\" : [NSDate date],\n"
  647. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  648. "};");
  649. verifyFormat(
  650. "@{\n"
  651. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  652. "regularFont,\n"
  653. "};");
  654. verifyFormat(
  655. "@{\n"
  656. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  657. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  658. "};");
  659. // We should try to be robust in case someone forgets the "@".
  660. verifyFormat("NSDictionary *d = {\n"
  661. " @\"nam\" : NSUserNam(),\n"
  662. " @\"dte\" : [NSDate date],\n"
  663. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  664. "};");
  665. verifyFormat("NSMutableDictionary *dictionary =\n"
  666. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  667. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  668. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  669. " cccccccccccccccc : ccccccccccccccc\n"
  670. " }];");
  671. // Ensure that casts before the key are kept on the same line as the key.
  672. verifyFormat(
  673. "NSDictionary *d = @{\n"
  674. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  675. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  676. "};");
  677. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  678. verifyFormat(
  679. "@{\n"
  680. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  681. "regularFont,\n"
  682. "};");
  683. }
  684. TEST_F(FormatTestObjC, ObjCArrayLiterals) {
  685. verifyIncompleteFormat("@[");
  686. verifyFormat("@[]");
  687. verifyFormat(
  688. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  689. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  690. verifyFormat("NSArray *array = @[ [foo description] ];");
  691. verifyFormat(
  692. "NSArray *some_variable = @[\n"
  693. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  694. " @\"aaaaaaaaaaaaaaaaa\",\n"
  695. " @\"aaaaaaaaaaaaaaaaa\",\n"
  696. " @\"aaaaaaaaaaaaaaaaa\",\n"
  697. "];");
  698. verifyFormat(
  699. "NSArray *some_variable = @[\n"
  700. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  701. " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
  702. "];");
  703. verifyFormat("NSArray *some_variable = @[\n"
  704. " @\"aaaaaaaaaaaaaaaaa\",\n"
  705. " @\"aaaaaaaaaaaaaaaaa\",\n"
  706. " @\"aaaaaaaaaaaaaaaaa\",\n"
  707. " @\"aaaaaaaaaaaaaaaaa\",\n"
  708. "];");
  709. verifyFormat("NSArray *array = @[\n"
  710. " @\"a\",\n"
  711. " @\"a\",\n" // Trailing comma -> one per line.
  712. "];");
  713. // We should try to be robust in case someone forgets the "@".
  714. verifyFormat("NSArray *some_variable = [\n"
  715. " @\"aaaaaaaaaaaaaaaaa\",\n"
  716. " @\"aaaaaaaaaaaaaaaaa\",\n"
  717. " @\"aaaaaaaaaaaaaaaaa\",\n"
  718. " @\"aaaaaaaaaaaaaaaaa\",\n"
  719. "];");
  720. verifyFormat(
  721. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  722. " index:(NSUInteger)index\n"
  723. " nonDigitAttributes:\n"
  724. " (NSDictionary *)noDigitAttributes;");
  725. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  726. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  727. "]];");
  728. }
  729. } // end namespace
  730. } // end namespace format
  731. } // end namespace clang