FormatTestObjC.cpp 50 KB

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