FormatTestSelective.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. //===- unittest/Format/FormatTestSelective.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 "FormatTestUtils.h"
  9. #include "clang/Format/Format.h"
  10. #include "llvm/Support/Debug.h"
  11. #include "gtest/gtest.h"
  12. #define DEBUG_TYPE "format-test"
  13. namespace clang {
  14. namespace format {
  15. namespace {
  16. class FormatTestSelective : public ::testing::Test {
  17. protected:
  18. std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) {
  19. LLVM_DEBUG(llvm::errs() << "---\n");
  20. LLVM_DEBUG(llvm::errs() << Code << "\n\n");
  21. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  22. FormattingAttemptStatus Status;
  23. tooling::Replacements Replaces =
  24. reformat(Style, Code, Ranges, "<stdin>", &Status);
  25. EXPECT_TRUE(Status.FormatComplete) << Code << "\n\n";
  26. auto Result = applyAllReplacements(Code, Replaces);
  27. EXPECT_TRUE(static_cast<bool>(Result));
  28. LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
  29. return *Result;
  30. }
  31. FormatStyle Style = getLLVMStyle();
  32. };
  33. TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) {
  34. EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0));
  35. EXPECT_EQ("int a;", format("int a; ", 0, 0));
  36. EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0));
  37. EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0));
  38. }
  39. TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
  40. EXPECT_EQ("{int b;\n"
  41. " int a;\n"
  42. "}",
  43. format("{int b;\n int a;}", 8, 0));
  44. EXPECT_EQ("{\n"
  45. " int b;\n"
  46. " int a;}",
  47. format("{int b;\n int a;}", 7, 0));
  48. Style.ColumnLimit = 12;
  49. EXPECT_EQ("#define A \\\n"
  50. " int a; \\\n"
  51. " int b;",
  52. format("#define A \\\n"
  53. " int a; \\\n"
  54. " int b;",
  55. 26, 0));
  56. EXPECT_EQ("#define A \\\n"
  57. " int a; \\\n"
  58. " int b;",
  59. format("#define A \\\n"
  60. " int a; \\\n"
  61. " int b;",
  62. 25, 0));
  63. }
  64. TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) {
  65. EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0));
  66. EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0));
  67. // This might not strictly be correct, but is likely good in all practical
  68. // cases.
  69. EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0));
  70. }
  71. TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  72. EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0));
  73. EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0));
  74. }
  75. TEST_F(FormatTestSelective, ReformatsMovedLines) {
  76. EXPECT_EQ(
  77. "template <typename T> T *getFETokenInfo() const {\n"
  78. " return static_cast<T *>(FETokenInfo);\n"
  79. "}\n"
  80. "int a; // <- Should not be formatted",
  81. format(
  82. "template<typename T>\n"
  83. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  84. "int a; // <- Should not be formatted",
  85. 9, 5));
  86. }
  87. TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) {
  88. Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
  89. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
  90. EXPECT_EQ("if (a) return; // comment",
  91. format("if(a)\nreturn; // comment", 20, 1));
  92. }
  93. TEST_F(FormatTestSelective, FormatsCommentsLocally) {
  94. EXPECT_EQ("int a; // comment\n"
  95. "int b; // comment",
  96. format("int a; // comment\n"
  97. "int b; // comment",
  98. 0, 0));
  99. EXPECT_EQ("int a; // comment\n"
  100. " // line 2\n"
  101. "int b;",
  102. format("int a; // comment\n"
  103. " // line 2\n"
  104. "int b;",
  105. 28, 0));
  106. EXPECT_EQ("int a; // comment\n"
  107. "// comment 2\n"
  108. "int b;",
  109. format("int a; // comment\n"
  110. "// comment 2\n"
  111. "int b;", 28, 0));
  112. EXPECT_EQ("int aaaaaa; // comment\n"
  113. "int b;\n"
  114. "int c; // unrelated comment",
  115. format("int aaaaaa; // comment\n"
  116. "int b;\n"
  117. "int c; // unrelated comment",
  118. 31, 0));
  119. EXPECT_EQ("int a; // This\n"
  120. " // is\n"
  121. " // a",
  122. format("int a; // This\n"
  123. " // is\n"
  124. " // a",
  125. 0, 0));
  126. EXPECT_EQ("int a; // This\n"
  127. " // is\n"
  128. " // a\n"
  129. "// This is b\n"
  130. "int b;",
  131. format("int a; // This\n"
  132. " // is\n"
  133. " // a\n"
  134. "// This is b\n"
  135. "int b;",
  136. 0, 0));
  137. EXPECT_EQ("int a; // This\n"
  138. " // is\n"
  139. " // a\n"
  140. "\n"
  141. "//This is unrelated",
  142. format("int a; // This\n"
  143. " // is\n"
  144. " // a\n"
  145. "\n"
  146. "//This is unrelated",
  147. 0, 0));
  148. EXPECT_EQ("int a;\n"
  149. "// This is\n"
  150. "// not formatted. ",
  151. format("int a;\n"
  152. "// This is\n"
  153. "// not formatted. ",
  154. 0, 0));
  155. EXPECT_EQ("int x; // Format this line.\n"
  156. "int xx; //\n"
  157. "int xxxxx; //",
  158. format("int x; // Format this line.\n"
  159. "int xx; //\n"
  160. "int xxxxx; //",
  161. 0, 0));
  162. }
  163. TEST_F(FormatTestSelective, ContinueReindenting) {
  164. // When we change an indent, we continue formatting as long as following
  165. // lines are not indented correctly.
  166. EXPECT_EQ("int i;\n"
  167. "int b;\n"
  168. "int c;\n"
  169. "int d;\n"
  170. "int e;\n"
  171. " int f;\n",
  172. format("int i;\n"
  173. " int b;\n"
  174. " int c;\n"
  175. " int d;\n"
  176. "int e;\n"
  177. " int f;\n",
  178. 11, 0));
  179. }
  180. TEST_F(FormatTestSelective, ReindentClosingBrace) {
  181. EXPECT_EQ("int i;\n"
  182. "int f() {\n"
  183. " int a;\n"
  184. " int b;\n"
  185. "}\n"
  186. " int c;\n",
  187. format("int i;\n"
  188. " int f(){\n"
  189. "int a;\n"
  190. "int b;\n"
  191. " }\n"
  192. " int c;\n",
  193. 11, 0));
  194. EXPECT_EQ("void f() {\n"
  195. " if (foo) {\n"
  196. " b();\n"
  197. " } else {\n"
  198. " c();\n"
  199. " }\n"
  200. "int d;\n"
  201. "}\n",
  202. format("void f() {\n"
  203. " if (foo) {\n"
  204. "b();\n"
  205. "}else{\n"
  206. "c();\n"
  207. "}\n"
  208. "int d;\n"
  209. "}\n",
  210. 13, 0));
  211. EXPECT_EQ("int i = []() {\n"
  212. " class C {\n"
  213. " int a;\n"
  214. " int b;\n"
  215. " };\n"
  216. " int c;\n"
  217. "};\n",
  218. format("int i = []() {\n"
  219. " class C{\n"
  220. "int a;\n"
  221. "int b;\n"
  222. "};\n"
  223. "int c;\n"
  224. " };\n",
  225. 17, 0));
  226. }
  227. TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
  228. EXPECT_EQ("DEBUG({\n"
  229. " int i;\n"
  230. " int j;\n"
  231. "});",
  232. format("DEBUG( {\n"
  233. " int i;\n"
  234. " int j;\n"
  235. "} ) ;",
  236. 20, 1));
  237. EXPECT_EQ("DEBUG( {\n"
  238. " int i;\n"
  239. " int j;\n"
  240. "} ) ;",
  241. format("DEBUG( {\n"
  242. " int i;\n"
  243. " int j;\n"
  244. "} ) ;",
  245. 41, 1));
  246. EXPECT_EQ("DEBUG( {\n"
  247. " int i;\n"
  248. " int j;\n"
  249. "} ) ;",
  250. format("DEBUG( {\n"
  251. " int i;\n"
  252. " int j;\n"
  253. "} ) ;",
  254. 41, 1));
  255. EXPECT_EQ("DEBUG({\n"
  256. " int i;\n"
  257. " int j;\n"
  258. "});",
  259. format("DEBUG( {\n"
  260. " int i;\n"
  261. " int j;\n"
  262. "} ) ;",
  263. 20, 1));
  264. EXPECT_EQ("Debug({\n"
  265. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  266. " return;\n"
  267. " },\n"
  268. " a);",
  269. format("Debug({\n"
  270. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  271. " return;\n"
  272. " },\n"
  273. " a);",
  274. 50, 1));
  275. EXPECT_EQ("DEBUG({\n"
  276. " DEBUG({\n"
  277. " int a;\n"
  278. " int b;\n"
  279. " }) ;\n"
  280. "});",
  281. format("DEBUG({\n"
  282. " DEBUG({\n"
  283. " int a;\n"
  284. " int b;\n" // Format this line only.
  285. " }) ;\n" // Don't touch this line.
  286. "});",
  287. 35, 0));
  288. EXPECT_EQ("DEBUG({\n"
  289. " int a; //\n"
  290. "});",
  291. format("DEBUG({\n"
  292. " int a; //\n"
  293. "});",
  294. 0, 0));
  295. EXPECT_EQ("someFunction(\n"
  296. " [] {\n"
  297. " // Only with this comment.\n"
  298. " int i; // invoke formatting here.\n"
  299. " }, // force line break\n"
  300. " aaa);",
  301. format("someFunction(\n"
  302. " [] {\n"
  303. " // Only with this comment.\n"
  304. " int i; // invoke formatting here.\n"
  305. " }, // force line break\n"
  306. " aaa);",
  307. 63, 1));
  308. EXPECT_EQ("int longlongname; // comment\n"
  309. "int x = f({\n"
  310. " int x; // comment\n"
  311. " int y; // comment\n"
  312. "});",
  313. format("int longlongname; // comment\n"
  314. "int x = f({\n"
  315. " int x; // comment\n"
  316. " int y; // comment\n"
  317. "});",
  318. 65, 0));
  319. EXPECT_EQ("int s = f({\n"
  320. " class X {\n"
  321. " public:\n"
  322. " void f();\n"
  323. " };\n"
  324. "});",
  325. format("int s = f({\n"
  326. " class X {\n"
  327. " public:\n"
  328. " void f();\n"
  329. " };\n"
  330. "});",
  331. 0, 0));
  332. EXPECT_EQ("SomeFunction(\n"
  333. " [] {\n"
  334. " int i;\n"
  335. " return i;\n" // Format this line.
  336. " },\n"
  337. " [] {\n"
  338. " return 2;\n" // Don't fix this.
  339. " });",
  340. format("SomeFunction(\n"
  341. " [] {\n"
  342. " int i;\n"
  343. " return i;\n" // Format this line.
  344. " },\n"
  345. " [] {\n"
  346. " return 2;\n" // Don't fix this.
  347. " });",
  348. 40, 0));
  349. }
  350. TEST_F(FormatTestSelective, WrongIndent) {
  351. EXPECT_EQ("namespace {\n"
  352. "int i;\n"
  353. "int j;\n"
  354. "}",
  355. format("namespace {\n"
  356. " int i;\n" // Format here.
  357. " int j;\n"
  358. "}",
  359. 15, 0));
  360. EXPECT_EQ("namespace {\n"
  361. " int i;\n"
  362. " int j;\n"
  363. "}",
  364. format("namespace {\n"
  365. " int i;\n"
  366. " int j;\n" // Format here.
  367. "}",
  368. 24, 0));
  369. }
  370. TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
  371. Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
  372. EXPECT_EQ("int i;\n"
  373. "#define A \\\n"
  374. " int i; \\\n"
  375. " int j\n"
  376. "int k;",
  377. format("int i;\n"
  378. "#define A \\\n"
  379. " int i ; \\\n"
  380. " int j\n"
  381. "int k;",
  382. 8, 0)); // 8: position of "#define".
  383. EXPECT_EQ("int i;\n"
  384. "#define A \\\n"
  385. " int i; \\\n"
  386. " int j\n"
  387. "int k;",
  388. format("int i;\n"
  389. "#define A \\\n"
  390. " int i ; \\\n"
  391. " int j\n"
  392. "int k;",
  393. 45, 0)); // 45: position of "j".
  394. }
  395. TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
  396. EXPECT_EQ("{\n"
  397. "{\n"
  398. "a;\n"
  399. "b;\n"
  400. "}\n"
  401. "}",
  402. format("{\n"
  403. "{\n"
  404. "a;\n"
  405. " b;\n"
  406. "}\n"
  407. "}",
  408. 13, 2));
  409. EXPECT_EQ("{\n"
  410. "{\n"
  411. " a;\n"
  412. " b;\n"
  413. " c;\n"
  414. " d;\n"
  415. "}\n"
  416. "}",
  417. format("{\n"
  418. "{\n"
  419. " a;\n"
  420. " b;\n"
  421. " c;\n"
  422. " d;\n"
  423. "}\n"
  424. "}",
  425. 9, 2));
  426. EXPECT_EQ("{\n"
  427. "{\n"
  428. "public:\n"
  429. " b;\n"
  430. "}\n"
  431. "}",
  432. format("{\n"
  433. "{\n"
  434. "public:\n"
  435. " b;\n"
  436. "}\n"
  437. "}",
  438. 17, 2));
  439. EXPECT_EQ("{\n"
  440. "{\n"
  441. "a;\n"
  442. "}\n"
  443. "{\n"
  444. " b; //\n"
  445. "}\n"
  446. "}",
  447. format("{\n"
  448. "{\n"
  449. "a;\n"
  450. "}\n"
  451. "{\n"
  452. " b; //\n"
  453. "}\n"
  454. "}",
  455. 22, 2));
  456. EXPECT_EQ(" {\n"
  457. " a; //\n"
  458. " }",
  459. format(" {\n"
  460. "a; //\n"
  461. " }",
  462. 4, 2));
  463. EXPECT_EQ("void f() {}\n"
  464. "void g() {}",
  465. format("void f() {}\n"
  466. "void g() {}",
  467. 13, 0));
  468. EXPECT_EQ("int a; // comment\n"
  469. " // line 2\n"
  470. "int b;",
  471. format("int a; // comment\n"
  472. " // line 2\n"
  473. " int b;",
  474. 35, 0));
  475. EXPECT_EQ(" void f() {\n"
  476. "#define A 1\n"
  477. " }",
  478. format(" void f() {\n"
  479. " #define A 1\n" // Format this line.
  480. " }",
  481. 20, 0));
  482. EXPECT_EQ(" void f() {\n"
  483. " int i;\n"
  484. "#define A \\\n"
  485. " int i; \\\n"
  486. " int j;\n"
  487. " int k;\n"
  488. " }",
  489. format(" void f() {\n"
  490. " int i;\n"
  491. "#define A \\\n"
  492. " int i; \\\n"
  493. " int j;\n"
  494. " int k;\n" // Format this line.
  495. " }",
  496. 67, 0));
  497. Style.ColumnLimit = 11;
  498. EXPECT_EQ(" int a;\n"
  499. " void\n"
  500. " ffffff() {\n"
  501. " }",
  502. format(" int a;\n"
  503. "void ffffff() {}",
  504. 11, 0));
  505. }
  506. TEST_F(FormatTestSelective, UnderstandsTabs) {
  507. Style.IndentWidth = 8;
  508. Style.UseTab = FormatStyle::UT_Always;
  509. Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
  510. EXPECT_EQ("void f() {\n"
  511. "\tf();\n"
  512. "\tg();\n"
  513. "}",
  514. format("void f() {\n"
  515. "\tf();\n"
  516. "\tg();\n"
  517. "}",
  518. 0, 0));
  519. EXPECT_EQ("void f() {\n"
  520. "\tf();\n"
  521. "\tg();\n"
  522. "}",
  523. format("void f() {\n"
  524. "\tf();\n"
  525. "\tg();\n"
  526. "}",
  527. 16, 0));
  528. EXPECT_EQ("void f() {\n"
  529. " \tf();\n"
  530. "\tg();\n"
  531. "}",
  532. format("void f() {\n"
  533. " \tf();\n"
  534. " \tg();\n"
  535. "}",
  536. 21, 0));
  537. }
  538. TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
  539. EXPECT_EQ(
  540. "void f() {\n"
  541. " if (a) {\n"
  542. " g();\n"
  543. " h();\n"
  544. " }\n"
  545. "\n"
  546. "void g() {\n"
  547. "}",
  548. format("void f() {\n"
  549. " if (a) {\n" // Assume this was added without the closing brace.
  550. " g();\n"
  551. " h();\n"
  552. "}\n"
  553. "\n"
  554. "void g() {\n" // Make sure not to format this.
  555. "}",
  556. 15, 0));
  557. }
  558. TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) {
  559. Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  560. EXPECT_EQ(
  561. "var x = \"a\";\n"
  562. "var x = 'a';\n"
  563. "var x = \"a\";",
  564. format("var x = \"a\";\n"
  565. "var x = \"a\";\n"
  566. "var x = \"a\";",
  567. 20, 0));
  568. }
  569. TEST_F(FormatTestSelective, KeepsIndentAfterCommentSectionImport) {
  570. std::string Code = "#include <a> // line 1\n" // 23 chars long
  571. " // line 2\n" // 23 chars long
  572. "\n" // this newline is char 47
  573. "int i;"; // this line is not indented
  574. EXPECT_EQ(Code, format(Code, 47, 1));
  575. }
  576. } // end namespace
  577. } // end namespace format
  578. } // end namespace clang