FormatTestObjC.cpp 50 KB

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